Fcm with Aws Sns

whats the benefits of using AWS SNS with firebase cloud messaging(fcm) to send push notifications

SNS provides a few advantages over calling FCM directly:

  • Retries: if the API call to FCM to send the push notification fails for some reason (networking issue, FCM having availability issues, etc.), then SNS will retry the deliveries for you, so as long as you successfully publish the message to SNS, it will be delivered eventually.
  • Latency: publishing a message to SNS is asynchronous from its delivery and therefore is very fast, especially if you're doing this from within AWS. This has the benefit that if you have a blocking process that tries to publish a message to multiple devices, this process may finish a lot faster.
  • Throughput: if you wish to deliver a large number of messages quickly but do not have a lot of server capacity, letting SNS use its larger fleet to do the deliveries for you makes sense as it will result in more notifications getting delivered every second.
  • Broadcasts: using SNS topics, you can make a single API call to fan out a notification to millions of devices. If you wanted to do this yourself, you'd have to make one call to FCM for each device.
  • Device status: SNS will also keep track of which devices' tokens are now invalid and invalidate the associated PlatformEndpoints for you. This removes some metadata that you might otherwise need to keep track of.

Those are a few things that come to mind!

FCM with AWS SNS

FCM is backwards compatible with GCM. The steps for setting up FCM on AWS are identical to the GCM set up procedure and (at least for the moment) FCM works transparently with GCM and SNS with respect to server-side configuration.

However, if you are sending data payloads to the Android device they will not be processed unless you implement a client side service that extends FirebaseMessagingService. The default JSON message generator in the AWS console sends data messages, which will be ignored by your app unless the aforementioned service is implemented. To get around this for initial testing you can provide a custom notification payload which will be received by your device (as long as your app is not in the foreground)

There are GCM-FCM migration instructions provided by Google however the changes you need to make are predominantly on the App side.

The steps you need to follow to test GCM/FCM on your app with SNS are:

  1. Create a Platform Application in SNS, selecting Google Cloud Messaging (GCM) as the Push Notification Platform, and providing your Server API key in the API key field.
  2. Select the Platform Application and click the Create platform endpoint button.
  3. Provide the InstanceID (Device Token) generated by your app. You must extend the FirebaseInstanceIDService and override the onTokenRefresh method to see this within your Android App. Once you have done this, uninstall and reinstall your app and your token should be printed to the Debug console in Android Studio on first boot.
  4. Click the Add endpoint button.
  5. Click on the ARN link for your platform application.
  6. Select the newly created Endpoint for your device and click the Publish to endpoint button.
  7. Select the JSON Message Format, and click the JSON message generator button.
  8. Enter a test message and click the Generate JSON button
  9. Now comes the "gotcha part".

The message that is generated by SNS will be of the form:

{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}

As we mentioned earlier, data payloads will be ignored if no service to receive them has been implemented. We would like to test without writing too much code, so instead we should send a notification payload. To do this, simply change the JSON message to read:

{
"GCM": "{ \"notification\": { \"title\": \"test title\", \"body\": \"test body\" } }"
}

(For more information about the JSON format of an FCM message, see the FCM documentation.)

Once you have done this, make sure your app is not running on the device, and hit the Publish Message button. You should now see a notification pop up on your device.

You can of course do all this programmatically through the Amazon SNS API, however all the examples seem to use the data payload so you need to keep that in mind and generate a payload appropriate to your use case.

Amazon SNS with Firebase iOS Cloud Messaging Notifications not working

I found the solution:

The default format SNS sends when you publish messages to FCM works for android but not iOS. I had to send a custom payload to FCM in the format shown below and it worked! I would expect the payload being sent to FCM to work across both iOS/android but it does not.

{
"GCM": "{"notification": { "body": "Sample message for Android endpoints", "title":"Hello world" } }"
}

Good references:

https://stackoverflow.com/a/61166165/1123434

https://stackoverflow.com/a/38626398/1123434



Related Topics



Leave a reply



Submit