Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Extra parameters in payload via Amazon SNS Push Notification

This is something new i am asking as i haven't got it any answers for it on SO.

I am using Amazon SNS Push for sending push to my registered devices, everything is working good, i can register devices on my app first start, can send push etc etc. The problem i am facing is that, i want to open a specific page when i open my app through push. I want to send some extra params with the payload but i am not able to do that.

I tried this Link :- http://docs.aws.amazon.com/sns/latest/api/API_Publish.html

we have only one key i.e. "Message", in which we can pass the payload as far as i know.

i want pass a payload like this :-

{     aps = {             alert = "My Push text Msg";           };     "id" = "123",     "s" = "section" } 

or any other format is fine, i just wanted to pass 2-3 values along with payload so that i can use them in my app.

The code i am using for sending push is :-

// Load the AWS SDK for PHP if($_REQUEST) {     $title=$_REQUEST["push_text"];      if($title!="")     {         require 'aws-sdk.phar';           // Create a new Amazon SNS client         $sns = Aws\Sns\SnsClient::factory(array(             'key'    => '...',             'secret' => '...',             'region' => 'us-east-1'         ));          // Get and display the platform applications         //print("List All Platform Applications:\n");         $Model1 = $sns->listPlatformApplications();          print("\n</br></br>");*/          // Get the Arn of the first application         $AppArn = $Model1['PlatformApplications'][0]['PlatformApplicationArn'];          // Get the application's endpoints         $Model2 = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $AppArn));          // Display all of the endpoints for the first application         //print("List All Endpoints for First App:\n");         foreach ($Model2['Endpoints'] as $Endpoint)         {           $EndpointArn = $Endpoint['EndpointArn'];           //print($EndpointArn . "\n");         }         //print("\n</br></br>");          // Send a message to each endpoint         //print("Send Message to all Endpoints:\n");         foreach ($Model2['Endpoints'] as $Endpoint)         {           $EndpointArn = $Endpoint['EndpointArn'];            try           {             $sns->publish(array('Message' => $title,                     'TargetArn' => $EndpointArn));              //print($EndpointArn . " - Succeeded!\n");           }           catch (Exception $e)           {             //print($EndpointArn . " - Failed: " . $e->getMessage() . "!\n");           }         }     } } ?> 

Any help or idea will be appreciated. Thanks in advance.

like image 214
mAc Avatar asked Sep 17 '13 09:09

mAc


People also ask

What is the maximum possible size of a single message sent through Amazon SNS?

Today, we are increasing the maximum allowed payload size for the Amazon Simple Queue Service (SQS) and the Amazon Simple Notification Service (SNS) from 64 KB to 256 KB.

Which AWS hosted messaging service can be used to deliver push notifications?

Amazon SNS provides redundancy across multiple SMS providers, and enables you to send mobile push notifications using a single API for all mobile platforms. To learn more, see SMS, Mobile Push Notifications, and Email Notifications.

Is Amazon SNS scalable?

Since Amazon SNS is both highly reliable and scalable, it provides significant advantages to developers who build applications that rely on real-time events.

What is difference between SQS and SNS?

SQS is mainly used to decouple applications or integrate applications. SNS is used to broadcast messages and it's up to the receivers how they interpret and process those messages.


2 Answers

The Amazon SNS documentation is still lacking here, with few pointers on how to format a message to use a custom payload. This FAQ explains how to do it, but doesn't provide an example.

The solution is to publish the notification with the MessageStructure parameter set to json and a Message parameter that is json-encoded, with a key for each transport protocol. There always needs to be a default key too, as a fallback.

This is an example for iOS notifications with a custom payload:

array(     'TargetArn' => $EndpointArn,     'MessageStructure' => 'json',     'Message' => json_encode(array(         'default' => $title,         'APNS' => json_encode(array(             'aps' => array(                 'alert' => $title,             ),             // Custom payload parameters can go here             'id' => '123',             's' => 'section'         ))      )) ); 

The same goes for other protocols as well. The format of the json_encoded message must be like this (but you can omit keys if you don't use the transport):

{      "default": "<enter your message here>",      "email": "<enter your message here>",      "sqs": "<enter your message here>",      "http": "<enter your message here>",      "https": "<enter your message here>",      "sms": "<enter your message here>",      "APNS": "{\"aps\":{\"alert\": \"<message>\",\"sound\":\"default\"} }",      "APNS_SANDBOX": "{\"aps\":{\"alert\": \"<message>\",\"sound\":\"default\"} }",      "GCM": "{ \"data\": { \"message\": \"<message>\" } }",      "ADM": "{ \"data\": { \"message\": \"<message>\" } }"  } 
like image 61
felixdv Avatar answered Oct 04 '22 20:10

felixdv


From a Lambda function (Node.js) the call should be:

exports.handler = function(event, context) {    var params = {     'TargetArn' : $EndpointArn,     'MessageStructure' : 'json',     'Message' : JSON.stringify({       'default' : $title,       'APNS' : JSON.stringify({         'aps' : {            'alert' : $title,           'badge' : '0',           'sound' : 'default'         },         'id' : '123',         's' : 'section',       }),       'APNS_SANDBOX' : JSON.stringify({         'aps' : {            'alert' : $title,           'badge' : '0',           'sound' : 'default'         },         'id' : '123',         's' : 'section',       })     })   };    var sns = new AWS.SNS({apiVersion: '2010-03-31', region: 'us-east-1' });   sns.publish(params, function(err, data) {     if (err) {       // Error       context.fail(err);     }     else {       // Success       context.succeed();     }   }); }

You can simplify by specifying only one protocol: APNS or APNS_SANDBOX.

like image 20
rjobidon Avatar answered Oct 04 '22 20:10

rjobidon