Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message multiple platforms through AWS SNS with aws-ruby-sdk

I'm trying to use Amazon's Ruby SDK to send out push notifications with additional attributes for different platforms, namely APNS and GCM (Apple and Android devices), but I cannot find a way to send the documented message structure using the SDK's API.

To send a message with different payloads for GCM and APNS, the docs I found suggest the following structure:

{
  "APNS": {
    "aps": {
      "alert": "someone set us up the bomb"
    }
  },
  "GCM": {
    "data": {
      "message": "we get signal"
    }
  }
}

I would expect SNS to properly send the platform-specific payload to the correct platform endpoints, but instead the message to, for example, an android device, also includes the APNS payload - it includes the full message body.

Here's how I'm sending the message:

sns = AWS::SNS::Client.new
sns.publish(
  target_arn: endpoint_arn,
  message: {
    GCM: ...,
    APNS: ...
  }.to_json
)

The available documentation, particularly for the ruby sdk, doesn't seem to be all that detailed. Am I incorrect in my assumption that the payload should be properly distributed depending on the endpoint platform? Is there a better way to achieve what I'm trying to do?

like image 424
Filipe Avatar asked Sep 30 '22 04:09

Filipe


2 Answers

Probably you've already figured this out, but here is how it works on PHP. Probably the message structure is the same is in Ruby:

publish(array(
        'TopicArn' => 'YOUR_TOPIC_ARN',
        'Message' => '{ 
                        "default": "YOUR_MESSAGE", 
                        "email": "YOUR_MESSAGE", 
                        "sqs": "YOUR_MESSAGE", 
                        "http": "YOUR_MESSAGE", 
                        "https": "YOUR_MESSAGE", 
                        "sms": "YOUR_MESSAGE", 
                        "APNS": "{\"aps\":{\"alert\": \"YOUR_MESSAGE\",\"sound\":\"default\", \"badge\":\"1\"} }", 
                        "GCM": "{ \"data\": { \"message\": \"YOUR_MESSAGE\" } }", 
                        "ADM": "{ \"data\": { \"message\": \"YOUR_MESSAGE\" } }"
                    }',
        'MessageStructure' => 'json'
    ));
like image 165
Gregorio Mascarpone Avatar answered Oct 11 '22 14:10

Gregorio Mascarpone


Old story, but here is how I managed to do it, after @user3719626 inspiration:

client.publish({
  target_arn: @endpoint_arn,
  message: {
    "default" => "Default message. Used only if the target does not support any other format.",
    "APNS"    => {
                   "aps" => {
                              "alert" => "Whatever!",
                              "url"   => "stackoverflow.com",
                            }
                 }.to_json,
    "GCM"     => {
                   "data" => {
                               "message" => "Whatever!",
                               "url"     => "stackoverflow.com"
                             }
                 }.to_json,
  }.to_json,
  message_structure: 'json',
})

Notes:

  • client is an instance of AWS::SNS::Client.
  • @endpoint_arn is a string containing the target endpoint / topic ARN identifier.
  • message must be a string, and here it must be a JSON string.
  • Each platform key must point to a JSON string as well. So each platform value is "JSONified" twice.
like image 27
Eric Platon Avatar answered Oct 11 '22 14:10

Eric Platon