Home » Android » android – Sending a notification-only payload to GCM using AWS SNS

android – Sending a notification-only payload to GCM using AWS SNS

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to send mobile push notifications to GCM via AWS SNS. According to the latest GCM 3.0 documentation, one may include either a “notification” payload or a “data” payload (or both). If you send a notification payload, then GCM will take care of showing the notification on the end-user device for you.

Using the Amazon SNS Console, I tried sending a notification-only payload, but I encountered the following error:

Invalid parameter: Message Reason: Invalid notification for protocol
GCM: data key is expected in the json message (Service: AmazonSNS;
Status Code: 400; Error Code: InvalidParameter)

I’m sending:

{
  "GCM":"{\"notification\":{\"title\":\"Test Message\"}}"
}

I suspect this might be an issue with SNS still conforming to a previous version of GCM and therefore it expects a “data” key, but I’m not sure. Is anyone else having a similar problem or have any experience with this? Thanks!

EDIT:
To clarify, I want to send GCM a notification payload and have it display an alert to the user automatically as described in the documentation. Right now, I’m beginning to wonder if SNS is even forwarding the notification payload to the device.

How to&Answers:

To answer my own question, I received the following from AWS Support on their forums:

You are correct, SNS does not currently support GCM’s “notification”
payload type as described in their documentation. SNS supports the
initial “data” payload type only but we are actively working on a
solution to support the “notification” type. In the meantime, the
“data” payload works the same except the client app is responsible for
processing data messages as it has in the past.

So until SNS supports the GCM notification payload, you have to handle the display of notifications yourself by extending the class GcmListenerService and overriding the method onMessageReceived.

UPDATE 10/2015:
It seems SNS has now implemented support for the GCM notification payload. If you include both the “data” and “notification” payloads, “notification” will take precedence. I only observed this behavior on SNS, but could not find any AWS blog/articles announcing this.