Push Notifications to Mobile Platforms using Amazon SNS
Push Notifications are the alert messages that can be sent by the app publishers to the app user even when they are not actively using an app. AWS SNS (Simple Notification Service) is a web service that manages the delivery or sending of notifications to subscribing endpoints.
For adding Push Notifications for Mobile platforms on AWS we need to create AWS account access keys. The access keys contain access key ID and secret access key as a set.
Steps to configure Push Notifications for Mobile platforms on AWS
- Login to your AWS account by using AWS account email address and password.
- Click on your account name in the navigation bar and select the “My Security Credentials” option from the dropdown menu.
- Then select the “Continue to Security Credentials”.
- Click on “Create New Access Key”. You can see a warning about to view or download the secret access key.
- Select “Show Access Key” to copy the “Access Key ID” and “Secret Access Key” from your browser window or choose “Download Key File” to download the “rootkey.csv” file that contains the “Access Key ID” and “Secret Access Key”.
To configure the AWS SNS push notification we need to install official the AWS SDK npm library for the NodeJs and configure the AWS credentials. Use the following command to install the node aws-sdk:
Configure AWS client with credentials
Create a Platform Application
After configuring the AWS credentials we need to Create Platform Application for both the iOS (Apple) and Android (Google) platform. We need to fill the Platform Application Name for the application and select the platform on which app is registered. For example, for an iOS app select Apple iOS/VOIP/mac and for an Android app select Firebase Cloud Messaging (FCM). Once your platform application name is created application ARN (Amazon Resource Name). It will use while creating a SNS topic for the registered devices.
Create Platform Application For an iOS app
We need to enter the credentials of your application uses to connect to the selected push notification platform. By uploading these credentials to Amazon SNS, you indicate that you have the right to use these credentials and that you allow Amazon SNS to use them on your behalf. If we want to use the sandbox for the development then we need to check the used for development sandbox checkbox. Select the push certificate type from the dropdown and upload the certificate file i.e. p12 file to load the credentials.
Platform Application For an Android app
For Android application, we need to add only the Google API key.
Create an SNS Topic
For creating SNS topic we need to register all the user’s device tokens with AWS SNS service. We can manage device token when user signup and login to the app and store the device tokens to the database. For each user device, the device token should be unique.
As we already have configured the AWS SDK, we can use the same for creating an SNS topic. We can create one topic for one user and topic name should be unique for each user.
Create Platform Endpoint
Now, create a platform endpoint for each device. For creating a platform endpoint requires platform application ARN that we got after creating a platform application and device token. Create platform endpoint returns an endpoint ARN which is used to subscribe to the topic ARN. If the platform endpoint already exists AWS does not create it again and return the existing platform endpoint.
To subscribe the device with the AWS SNS service we require the previously generated Topic ARN from SNS topic and Endpoint ARN from platform endpoint. The subscribe method returns subscription ARN.
Send Push Notifications
In AWS SNS push notification, the app user can create a topic. A publisher sends notifications to the topics. Amazon SNS service matches the topic to a list of subscribers who have subscribed to that topic and sends the notification to each of those subscribers. Subscribers receive all the notifications published to the topics to which they subscribe.
For sending the push notification we need to generate the message i.e. payload for each platform. Also, we can pass the data in the notification. For that, we need to add our custom payload.
Use the publish action to send the push notification to the mobile endpoint. You must specify the endpoint ARN of a targetArn parameter of the SNS service publish method.
Delete a subscription, only the owner of the topic can delete the subscription. We can manage the delete subscription when user log out from the app.