如何通过Amazon SNS推送通知在有效负载中发送额外参数

这是我正在问的新东西,因为我没有得到任何答案。

我正在使用亚马逊SNS推发送推送到我注册的设备,一切工作正常,我可以在我的应用程序首次启动注册设备,可以发送推等等等。我面临的问题是,我想打开一个特定的页面当我通过推开我的应用程序。 我想发送一些额外的有效载荷的参数,但我不能这样做。

我试过这个链接: – http://docs.aws.amazon.com/sns/latest/api/API_Publish.html

我们只有一个关键字,即“消息”,据我们所知,我们可以传递有效载荷。

我想通过这样的有效载荷: –

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

或任何其他格式是好的,我只是想传递2-3值与有效载荷,以便我可以在我的应用程序中使用它们。

我用来发送推送的代码是:

 // 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"); } } } } ?> 

任何帮助或想法将不胜感激。 提前致谢。

亚马逊的SNS文档在这里还是不多,几乎没有关于如何格式化消息来使用自定义有效载荷的指针。 这个FAQ解释了如何做,但没有提供一个例子。

解决scheme是发布通知,其中MessageStructure参数设置为json和一个用json编码的Message参数,每个传输协议都使用一个密钥。 作为回退,总是需要有一个default键。

这是一个自定义有效载荷的iOS通知的例子:

 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' )) )) ); 

其他协议也一样。 json_encoded消息的格式必须是这样的(但是如果你不使用传输,你可以省略键):

 { "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>\" } }" } 

从Lambda函数(Node.js)调用应该是:

 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(); } }); } 

只需指定一个协议即可简化: APNSAPNS_SANDBOX

我在这里没有经验的评论,但我想提请人们注意嵌套的json_encode。 如果没有它,APNSstring将不会被亚马逊分析,而只会发送默认的消息值。

我正在做以下工作:

 $message = json_encode(array( 'default'=>$msg, 'APNS'=>array( 'aps'=>array( 'alert'=>$msg, 'sound'=>'default' ), 'id'=>$id, 'other'=>$other ) ) ); 

但是这不行。 你必须按照felixdv的回答分别在'APNS'下json_encode数组。 不要问我为什么在我的控制台日志中输出看起来完全一样。 虽然文档显示在'APNS'键下的jsonstring应该被包裹在“”中,所以怀疑这与它有关。

http://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html

不要被愚弄,因为没有这些双引号的JSON将validation罚款。

另外我不确定emkman的评论。 如果没有上述结构中的“默认”键被发送到单个端点ARN,我会收到来自AWS的错误。

希望下午加快速度。

编辑

随后清除了嵌套json_encodes的需要 – 它创build了转义的双引号,尽pipe文档说API不是必需的,他们是整个string为GCM和上面的APNS下面的子arrays苹果。 这可能是我的实现,但是它使用AWS PHP SDK开箱即用,并且是使其发送自定义数据的唯一方法。