Getting started with the AWS SDK for Python (boto3) in Yandex Cloud Notification Service
boto3
To get started with the AWS SDK for Python (boto3):
- Prepare your cloud.
- Get a static access key.
- Configure the AWS SDK.
- 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.
Configure the AWS SDK
-
Install the AWS SDK for Python (boto3) using this command:
pip install boto3
-
Create a client:
import boto3 client = boto3.client( "sns", endpoint_url="https://notifications.yandexcloud.net/", region_name="ru-central1", aws_access_key_id="<static_key_ID>", aws_secret_access_key="<secret_key>", verify=False # Disable SSL verification for notifications.yandexcloud.net )
Where:
aws_access_key_id
: Static key ID.aws_secret_access_key
: Secret key.
Create a notification channel
response = client.create_platform_application(
Name="<channel_name>",
Platform="GCM",
Attributes= {
"PlatformCredential": "<FCM_API_key>"
}
)
print ("Platform application ARN:", response['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 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.The HTTP v1 API is preferred as FCM will no longer support
the Legacy API starting June 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.list_platform_applications()
for app in response["PlatformApplications"]:
print("Application ARN:", app["PlatformApplicationArn"])
You will get the list of notification channels located in the same folder as the service account.
Create an endpoint
response = client.create_platform_endpoint(
PlatformApplicationArn="<notification_channel_ARN>",
Token="<push_token>",
)
print ("Endpoint ARN:", response["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
Explicit notifications (Bright Push)
response = client.publish(
TargetArn="<endpoint_ID>",
Message=json.dumps({
"default": "<notification_text>",
"APNS": json.dumps({
"aps": {
"alert": "<notification_text>"
}
})
}),
MessageStructure="json"
)
print ("Message id:", response["MessageId"])
response = client.publish(
TargetArn="<endpoint_ID>",
Message=json.dumps({
"default": "<notification_text>",
"GCM": json.dumps({
"notification": {
"body": "<notification_text>"
}
})
}),
MessageStructure="json"
)
print ("Message id:", response["MessageId"])
Where:
TargetArn
: Mobile endpoint ID (ARN).MessageStructure
: Message format.Message
: Message.
Silent notifications (Silent Push)
response = client.publish(
TargetArn="<endpoint ARN>",
Message='{"data": { "key": "value" } }')
print ("Message id:", response["MessageId"])
Where:
TargetArn
: Mobile endpoint ID (ARN)Message
: Message