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

标签 php ios objective-c push-notification amazon

这是我要问的新问题,因为我在 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");
          }
        }
    }
}
?>

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

最佳答案

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

解决方案是发布通知,其中 MessageStructure 参数设置为 json 和一个 json 编码的 Message 参数,带有每个传输协议(protocol)的 key 。也总是需要一个 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'
        ))

    ))
);

其他协议(protocol)也是如此。 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>\" } }" 
}

关于php - 如何通过 Amazon SNS 推送通知在负载中发送额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18845984/

相关文章:

php - CodeIgniter:如果我使用 die() 函数,则不会加载 View

php - 两个函数不在同一 View Laravel 上运行

ios - 需要有关 Parse 中 IAP 类设计的建议

objective-c - 我如何为 IOS 简化这个 Objective-C 代码?

ios - NSString:删除 <b></b> 并为该段制作一个带有颜色的属性字符串?

ios - 为什么 dequeueReusableCellWithIdentifier 会导致我的整个应用程序崩溃/挂起?

php - 通过 allow_url_fopen=0 中的服务器配置

php - 点击 ="USER._like(' 16 1', this);"

objective-c - iOS 3.0 中 MKMapview 上带有按钮的自定义标注 View

ios - 在 RxSwift 中手动发出事件