How to get started with the AWS SDK for PHP in Yandex Cloud Notification Service
To get started with the AWS SDK for PHP:
- Get your cloud ready.
- Get a static access key.
- Configure the AWS SDK.
- Create a notification channel.
- Get a list of channels.
- Create an endpoint.
- Send a notification.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Get a static access key
For authentication in Cloud Notification Service, use a static access key. The key is issued for the service account, and all actions are performed on behalf of that service account.
To get a static access key:
-
Create a service account.
-
Create a static access key for the service account.
Save the ID and private key.
Configure the AWS SDK
-
Review the system requirements
. -
Install PHP
5.5 or higher.When using the AWS SDK, do not disable the SimpleXML PHP extension
. -
Install Composer
to manage PHP dependencies. -
Add the AWS SDK library for PHP into your project:
-
If Composer in your system is installed globally, run this command in your project's root folder:
composer require aws/aws-sdk-php
-
If Composer is not installed globally, run this command:
php composer.phar require aws/aws-sdk-php
For other installation methods, see the AWS documentation
. -
-
Create a client:
<?php require '/<path_to_folder>/vendor/autoload.php'; use Aws\Sns\SnsClient; $client = new SnsClient( [ 'endpoint' => 'https://notifications.yandexcloud.net/', 'version' => 'latest', 'region' => 'ru-central1', 'credentials' => [ 'key' => '<static_key_ID>', 'secret' => '<secret_key>', ], ] ); ?>
Where:
/<path_to_folder>/vendor/autoload.php
: Path to theautoload.php
file created when installing the AWS SDK.key
: Static key ID.secret
: Secret key.
Create a notification channel
$response = $client->createPlatformApplication(
[
'Name' => '<channel_name>',
'Platform' => 'GCM',
'Attributes' => [
'PlatformCredential' => '<FCM_API_key>',
],
]
);
print($response->get('PlatformApplicationArn'));
Where:
Name
: Notification channel name, user-defined. The name must be unique within the cloud. It may contain lowercase and uppercase Latin letters, numbers, underscores, hyphens, and periods. It may be from 1 to 256 characters long. For APNs channels, we recommend specifying the bundle ID in the name, and for FCM and HMS, the full package name.Platform
: Mobile platform type:APNS
andAPNS_SANDBOX
: Apple Push Notification service (APNs). UseAPNS_SANDBOX
to test the application.GCM
: Firebase Cloud Messaging (FCM).HMS
: Huawei Mobile Services (HMS).
Attributes
: Mobile platform authentication parameters inkey=value
format. The values depend on platform:-
APNs:
- Token-based authentication:
PlatformPrincipal
: Path to the token signature key file from Apple.PlatformCredential
: Key ID.ApplePlatformTeamID
: Team ID.ApplePlatformBundleID
: Bundle ID.
- Certificate-based authentication:
-
PlatformPrincipal
: SSL certificate in.pem
format. -
PlatformCredential
: Certificate private key in.pem
format.To save the certificate and the private key in individual
.pem
files, use the openssl Linux utility:openssl pkcs12 -in Certificates.p12 -nokeys -nodes -out certificate.pem openssl pkcs12 -in Certificates.p12 -nocerts -nodes -out privatekey.pem
-
Token-based authentication: The more modern and secure method.
- Token-based authentication:
-
FCM:
PlatformCredential
is the Google Cloud service account key in JSON format for authentication with the HTTP v1 API or API key (server key) for authentication with the legacy API.Use the HTTP v1 API because the FCM legacy API is no longer supported
starting July 2024. -
HMS:
PlatformPrincipal
: Key IDPlatformCredential
: API key
-
As a result, you will get a notification channel ID (ARN).
Get a list of notification channels
$response = $client->listPlatformApplications();
var_dump($response->get('PlatformApplications'));
You will get the list of notification channels located in the same folder as the service account.
Create an endpoint
$response = $client->createPlatformEndpoint(
[
'PlatformApplicationArn' => '<notification_channel_ARN>',
'Token' => '<push_token>',
]
);
print($response->get('EndpointArn'));
Where:
PlatformApplicationArn
: Notification channel ID (ARN).Token
: Unique push token for the app on the user’s device.
As a result, you will get a mobile endpoint ID (ARN).
Send a notification
Mobile push notifications
$response = $client->publish(
[
'TargetArn' => '<endpoint_ID>',
'Message' => json_encode([
'default' => '<notification_text>',
'APNS' => json_encode([
'aps' => [
'alert' => '<notification_text>',
],
])
]),
'MessageStructure' => 'json',
]
);
print($response->get('MessageId'));
Explicit notifications (Bright Push)
$response = $client->publish(
[
'TargetArn' => '<endpoint_ID>',
'Message' => json_encode([
'default' => '<notification_text>',
'GCM' => json_encode([
'notification' => [
'title' => '<notification_title>',
'body' => '<notification_text>',
],
])
]),
'MessageStructure' => 'json',
]
);
print($response->get('MessageId'));
Silent notifications (Silent Push)
$response = $client->publish(
[
'TargetArn' => '<endpoint_ID>',
'Message' => json_encode([
'default' => '<notification_text>',
'GCM' => json_encode([
'data' => ['<key>' => '<value>'],
])
]),
'MessageStructure' => 'json',
]
);
print($response->get('MessageId'));
Where:
TargetArn
: Mobile endpoint ID (ARN)MessageStructure
: Message formatMessage
: Message
Text message
$response = $client->publish(
[
'Message' => '<message>',
'PhoneNumber' => '<phone_number>',
]
);
print($response->get('MessageId'));