Creating an endpoint
For each channel, you need to create a set of endpoints through which to send notifications to specific users' browsers.
To create an endpoint:
-
From the user's browser, use Service Worker
to call the JavaScript subscribe method.The PushSubscription
object will be returned in response. -
Convert the object you got into JSON by calling the toJSON
method.
JSON response format:
{
"endpoint": <string>,
"expirationTime": <DOMHighResTimeStamp,null>,
"keys": {
"p256dh": <string>,
"auth": <string>
}
}
Where:
endpoint
: URL the push notification will be sent to. Typically this is the address of a push notification server, e.g., Firebase Cloud Messaging (FCM).expirationTime
: Notification lifetime in milliseconds. Thenull
value represents unlimited lifetime.keys
: Notification protection data:p256dh
: User's public key used to encrypt notification data.auth
: Authentication key to confirm the identity of the sender.
Example response in JSON
{
"endpoint": "https://fcm.googleapis.com/fcm/send/abcdef123456",
"expirationTime": 1704093740000,
"keys": {
"p256dh": "BOrLkr7sEt8tERyAv6c8ZG5UC********",
"auth": "aBcDeFg12345"
}
}
Once you have the data you need to send to notifications to a user, create an endpoint:
- In the management console
, go to the folder containing the notification channel. - From the list of services, select Cloud Notification Service.
- Select the Push notifications in browser tab.
- Select a notification channel.
- Select the
Endpoints tab. - Click Create endpoint.
- Enter the Endpoint parameters in JSON received when subscribing the user.
- Optionally, enter User data, a UTF-8
encoded text up to 2,048 characters long. - Click Create.
-
If you do not have the AWS CLI yet, install and configure it.
-
Run this command:
aws sns create-platform-endpoint \ --platform-application-arn <channel_ARN> \ --token <pushSubscription_JSON>
Where:
--platform-application-arn
: Notification channel ID (ARN).--token
: PushSubscription object, in JSON format received in the user's browser.
For more information about the
aws sns create-platform-endpoint
command, see the AWS documentation .
-
If you do not have the AWS SDK for Python (boto3) yet, install and configure it.
-
To create an endpoint, use the following code:
try: response = client.create_platform_endpoint( PlatformApplicationArn="<channel_ARN>", Token="<pushSubscription_JSON>", ) print(f'Endpoint ARN: {response["EndpointArn"]}') except botocore.exceptions.ClientError as error: print(f"Error: {error}")
-
If you do not have the AWS SDK for PHP yet, install and configure it.
-
To create an endpoint, use the following code:
$response = $client->createPlatformApplication( [ 'PlatformApplicationArn' => '<channel_ARN>', 'Token' => '<pushSubscription_JSON>', ] ); print('Endpoint ARN: ' . $response->get('EndpointArn'));
-
If you do not have the AWS SDK for JavaScript yet, install and configure it.
-
To create an endpoint, use the following code:
try { const response = await client.send( new AWS.CreatePlatformEndpointCommand({ PlatformApplicationArn: "<channel_ARN>", Token: "<pushSubscription_JSON>", }), ); console.log("Endpoint ARN:", response["EndpointArn"]); } catch (e) { console.log("Error:", e) }
Use the create HTTP API method for the Endpoint resource in Yandex Cloud Notification Service.
Creating an endpoint may take some time.