【问题标题】:How to send Extra parameters in payload via Amazon SNS Push Notification如何通过 Amazon SNS 推送通知在负载中发送额外参数
【发布时间】:2013-09-21 15:55:26
【问题描述】:

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

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

我试过这个链接:- 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");
          }
        }
    }
}
?>

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

【问题讨论】:

  • 嗯,我得到了解决方案,亚马逊应该在他们的实施文档中提到它,很快我会上传答案。
  • 嗨,请分享解决方案,我遇到了同样的问题 :)
  • 您找到问题所在了吗?我也有同样的事情——我能发送的只是默认消息。我在不同的平台上但同样的问题 - stackoverflow.com/q/22366310/850969
  • 解决方法如下,您还有什么问题吗??
  • 嘿,我在哪里可以下载“aws-sdk.phar”

标签: php ios objective-c push-notification amazon


【解决方案1】:

这里仍然缺少 Amazon SNS 文档,几乎没有关于如何格式化消息以使用自定义负载的指示。 This FAQ 解释了如何做,但没有提供示例。

解决方案是发布通知,其中MessageStructure 参数设置为jsonMessage 参数是 json 编码的,每个传输协议都有一个密钥。也总是需要一个 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>\" } }" 
}

【讨论】:

  • 直接发布到移动端点时,不需要默认密钥。需要有一个default 键或值GCMAPNSAPNS_SANDBOX 的键。省略默认键将为您节省字节,因为所有键和值都计入 256kb 限制。
  • 这里是文档的链接docs.aws.amazon.com/sns/latest/dg/…我花了很长时间才找到它...
  • 是否可以为 GCM 设置声音?
  • 嘿@felixdv...你能看看这个stackoverflow.com/questions/43079796/…非常感谢!
【解决方案2】:

来自 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

【讨论】:

  • 这在大多数情况下都有效,除非您放错了正确的键值对。它应该与 alert 键值对一起放在 aps 对象中。
  • @user3344977 谢谢,我更正了我的答案。 APNS 官方文档:developer.apple.com/library/content/documentation/…
  • 大家好,我有一个问题,设备端没有收到我的自定义数据,而且键“声音”的值未定义。有什么想法吗 ? @rjobidon
  • 你好,@Vince。我不明白为什么,因为亚马逊文档说您可以在特定于平台的部分和 JSON 有效负载中发送自定义键值对。 docs.aws.amazon.com/sns/latest/dg/… 请在 application(_:didReceiveRemoteNotification:) 回调中检查您的有效负载。
  • @rjobidon 我的解决方案是确保我拥有正确的有效负载。我正在向通过试飞分发的应用程序发送 PN,但我的服务器仍在将消息有效负载构造为“APNS_SANDBOX”而不是“APNS”。给其他人的建议,确保您的有效负载针对正确的环境。
【解决方案3】:

我太缺乏经验,无法在这里发表评论,但我想提请人们注意嵌套的 json_encode。这很重要,没有它,亚马逊将不会解析 APNS 字符串,它只会发送默认消息值。

我正在做以下事情:

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

但这行不通。您必须单独对“APNS”下的数组进行 json_encode,如 felixdv 的答案所示。不要问我为什么输出在我的控制台日志中看起来完全一样。虽然文档显示 'APNS' 键下的 json 字符串应该包含在 "" 中,所以怀疑这与它有关。

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

不要被愚弄,因为没有这些双引号,JSON 将很好地验证。

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

希望能加快某人的下午。

编辑

随后清除了嵌套 json_encodes 的需要 - 它创建了转义的双引号,尽管文档说 API 不需要它们,但它们适用于 GCM 的整个字符串,以及苹果的 APNS 下的子数组。这可能是我的实现,但它几乎是使用 AWS PHP SDK 开箱即用的,并且是使它发送自定义数据的唯一方法。

【讨论】:

    【解决方案4】:

    很容易错过您需要添加 APNS_SANDBOX 以及 APNS 以进行本地测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多