Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a notification-only payload to GCM using AWS SNS

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.

like image 405
weiy Avatar asked Aug 07 '15 17:08

weiy


People also ask

What happens to Amazon SNS metadata when I enable raw message delivery?

When you enable raw message delivery for Amazon Kinesis Data Firehose or Amazon SQS endpoints, any Amazon SNS metadata is stripped from the published message and the message is sent as is.

Can Amazon SNS send notifications to my APNs?

Amazon SNS can send messages to APNs as alert or background notifications (for more information, see Pushing Background Updates to Your App in the APNs documentation). An alert APNs notification informs the user by displaying an alert message, playing a sound, or adding a badge to your application’s icon.

How do I send large payloads with Amazon SNS?

Amazon SNS and Amazon SQS let you send and receive large payloads (from 64 to 256 kilobytes in size). To send large payloads, you must use an AWS SDK that supports Signature Version 4. To avoid having Amazon SQS and HTTP/S endpoints process the JSON formatting of messages, Amazon SNS also allows raw message delivery:

Does AWS SNS support FCM?

AWS SNS does support FCM. No AWS does not store messages after sending push notifications. For a detailed tutorial on setting up FCM with SNS please read this article. Thanks for contributing an answer to Stack Overflow!


1 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.

like image 161
weiy Avatar answered Oct 03 '22 14:10

weiy