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

mAc*_*mAc 28 php amazon objective-c push-notification ios

这是我要问的新事物,因为我没有得到任何答案.

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

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

我们只有一个键,即"消息",据我所知,我们可以在其中传递有效载荷.

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

{
    aps = {
            alert = "My Push text Msg";
          };
    "id" = "123",
    "s" = "section"
}
Run Code Online (Sandbox Code Playgroud)

或任何其他格式是好的,我只想传递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");
          }
        }
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

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

小智 60

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

解决方案是使用MessageStructure参数set to jsonMessagejson编码的参数发布通知,并使用每个传输协议的密钥.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'
        ))

    ))
);
Run Code Online (Sandbox Code Playgroud)

其他协议也是如此.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>\" } }" 
}
Run Code Online (Sandbox Code Playgroud)

  • 直接发布到移动端点时,不需要是默认密钥.需要一个`default`键或一个值为'GCM`,'APNS`或'APNS_SANDBOX`的键.省略默认密钥将节省您的字节,因为所有密钥和值都计入256kb限制. (3认同)

rjo*_*don 7

从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();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

您可以通过仅指定一个协议来简化:APNSAPNS_SANDBOX.