如何通过Amazon SNS推送通知在有效负载中发送额外参数 [英] How to send Extra parameters in payload via Amazon SNS Push Notification

查看:150
本文介绍了如何通过Amazon SNS推送通知在有效负载中发送额外参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要问的新内容,因为我在SO上没有得到任何答案。

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

我正在使用Amazon SNS Push向我发送推送注册设备,一切都运行良好,我可以在我的应用程序首先注册设备,可以发送推送等等。我面临的问题是,我想打开一个特定的页面,当我通过推动打开我的应用程序。我想用有效载荷发送一些额外的参数但是我无法做到这一点。

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.

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

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

或任何其他格式都没问题,我只想传递2-3个值以及有效负载,以便我可以在我的应用中使用它们。

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.

推荐答案

此处仍缺少Amazon SNS文档,几乎没有关于如何格式化消息以使用自定义负载。 此常见问题解答说明了如何操作,但未提供示例。

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.

解决方案是发布通知,并将 MessageStructure 参数设置为 json 和一个消息参数,它是json编码的,带有每个传输协议的密钥。总是需要一个默认键作为后备。

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.

这是带有自定义有效负载的iOS通知的示例:

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

    ))
);

其他协议也是如此。 json_encoded消息的格式必须与此类似(但如果不使用传输,则可以省略密钥):

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

这篇关于如何通过Amazon SNS推送通知在有效负载中发送额外参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆