Getting started with Yandex Cloud Notification Service using AWS CLI
Note
The service is at the preview stage.
To enable Cloud Notification Service, request access to the service from your account manager or technical support
Cloud Notification Service (CNS) is a service for multichannel notifications of users. The service's HTTP API is compatible with the Amazon SNS API
At the preview stage, you can send personalized push notifications to iOS and Android devices as well as text messages (SMS).
Moving forward, we will add notifications to messengers and browsers.
With Cloud Notification Service, you can send notifications to apps registered in the following services:
- Apple Push Notification service
(APNs). - Firebase Cloud Messaging
(FCM). - Huawei Mobile Services
(HMS).
To get started with the AWS CLI:
- Prepare your cloud.
- Get a static access key.
- Set up the AWS CLI.
- Create a notification channel.
- Get a list of channels.
- Create an endpoint.
- Send a notification.
Prepare your cloud
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.
Set up the AWS CLI
The AWS CLI
To configure the AWS CLI:
-
Install
the AWS CLI. -
Enter the command to configure the AWS CLI:
aws configure
-
Enter the values for these parameters:
-
AWS Access Key ID
: Static key ID -
AWS Secret Access Key
: Secret key -
Default region name
:ru-central1
Note
To work with Cloud Notification Service, always specify
ru-central1
as the region. A different region value may lead to an authorization error.
-
-
Leave the other parameter values unchanged.
-
Set the Cloud Notification Service endpoint:
aws configure set endpoint_url https://notifications.yandexcloud.net/
Sample configuration files
-
~/.aws/config
:[default] region = ru-central1 endpoint_url = https://notifications.yandexcloud.net/
-
~/.aws/credentials
:[default] aws_access_key_id = <static_key_ID> aws_secret_access_key = <secret_key>
For more information about setting up the AWS CLI, see the AWS documentation
Create a notification channel
To create a notification channel, run this command:
aws sns create-platform-application \
--name <channel_name> \
--platform GCM \
--attributes PlatformCredential=<FCM_API_key>
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 the platform type:-
APNs:
- Token-based authentication:
PlatformPrincipal
: Token in.p8
format.PlatformCredential
: Token ID.ApplePlatformTeamID
: Developer ID.ApplePlatformBundleID
: Bundle ID.
- Certificate-based authentication:
PlatformPrincipal
: SSL certificate in.p12
format.PlatformCredential
: Certificate private key.
Token-based authentication is preferred as it is more modern.
- 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.We recommend escaping the file contents using the
jq @json <<< cat private_key.json
command, as the AWS CLI accepts this parameter in string format.The HTTP v1 API is preferred as FCM will no longer support
the legacy API starting from June 2024. -
HMS:
PlatformPrincipal
: Key IDPlatformCredential
: API key
-
As a result, you will get a notification channel ID (ARN). Save it for future use.
For more information about the aws sns create-platform-application
command, see the AWS documentation
Get a list of notification channels
aws sns list-platform-applications
You will get the list of notification channels located in the same folder as the service account.
For more information about the aws sns list-platform-applications
command, see the AWS documentation
Create an endpoint
To create a mobile endpoint, run the following command:
aws sns create-platform-endpoint \
--platform-application-arn <notification_channel_ARN> \
--token <Push_token>
Where:
--platform-application-arn
: Notification channel ID (ARN).--token
: Unique push token for the application on the user’s device.
As a result, you will get a mobile endpoint ID (ARN). Save it for future use.
For more information about the aws sns create-platform-endpoint
command, see the AWS documentation
Send a notification
Explicit notifications (Bright Push)
aws sns publish \
--target-arn "<endpoint_ARN>" \
--message-structure json \
--message '{"default": "<notification_text>", "APNS": "{ \"aps\": { \"alert\": \"<notification_text>\"} }" }'
aws sns publish \
--target-arn "<endpoint_ARN>" \
--message-structure json \
--message '{"default": "<notification_text>", "GCM": "{ \"notification\": { \"body\": \"<notification_text>\"} }" }'
Where:
--target-arn
: Mobile endpoint ID (ARN)--message-structure
: Message format--message
: Message
Silent notifications (Silent Push)
aws sns publish \
--target-arn <endpoint_ARN> \
--message-structure json \
--message '{"data": { "key": "value" } }'
Where:
--target-arn
: Mobile endpoint ID (ARN)--message-structure
: Message format--message
: Message
For more information about the aws sns publish
command, see the AWS documentation