简体   繁体   English

亚马逊SNS:JSON向Windows手机致敬

[英]Amazon SNS: JSON toasts to Windows Phones

When I use the Amazon SNS console to send toast messages to a Windows Phone 8 device (ie with the MPNS system), I can only send messages in text format. 当我使用Amazon SNS控制台向Windows Phone 8设备(即使用MPNS系统)发送Toast消息时,我只能以文本格式发送消息。 Selecting " Use platform specific JSON message dictionaries " and sending a JSON toast never seems to get to the device. 选择“ 使用特定于平台的JSON消息字典 ”并发送JSON toast似乎永远不会到达设备。 The default message that you see when you select the platform specific format is a tile notification message, and that does work. 选择平台特定格式时看到的默认消息是切片通知消息,这确实有效。

For exmple, the following message neither gives an error nor is displayed in the device: 例如,以下消息既不会出错也不会显示在设备中:

{
"MPNS": "<?xml version=\"1.0\" encoding=\"utf-8\"?><wp:Notification xmlns:wp=\"WPNotification\"><wp:Toast><wp:Text1>Amazon</wp:Text1><wp:Text2>hooray</wp:Text2><wp:Param>this_is/my?extra=parameter</wp:Param></wp:Toast></wp:Notification>"
}

This has been tested with a couple of devices: Lumia 620 with Windows Phone 8.0 and Lumia 1020 with 8.1 beta. 这已经过一些设备测试:Lumia 620与Windows Phone 8.0和Lumia 1020与8.1 beta。 I have also tried sending messages from a Java backend, but it just shows up as a raw JSON text toast ( { "MPNS": ... ) again. 我也尝试从Java后端发送消息,但它只是再次显示为原始JSON文本吐司( { "MPNS": ... )。 What could possibly be wrong? 什么可能是错的? The JSON is valid, the XML is well-formed... I'm at loss. JSON是有效的,XML格式正确......我很茫然。

I have run into the same issue recently, and found a solution. 我最近遇到了同样的问题,并找到了解决方案。 The SNS documentation for MPNS does not emphasise an important step, but it can be found in the sample code and eventually in the message attributes section of the docs. MPNS的SNS文档并未强调重要的一步,但可以在示例代码中找到它,最终可以在文档的消息属性部分中找到。


You must set two MPNS-specific MessageAttributes on the PublishRequest. 必须在PublishRequest上设置两个MPNS特定的MessageAttributes。 Omitting them will cause the delivery to fail with no clues left to investigate: ie even if you enable delivery status logs with CloudWatch, providerResponse will be missing. 省略它们将导致交付失败,没有任何线索需要调查:即使您使用CloudWatch启用交付状态日志,也会丢失providerResponse

For reference, the attributes are the following: 作为参考,属性如下:

  1. Attribute name: AWS.SNS.MOBILE.MPNS.Type 属性名称: AWS.SNS.MOBILE.MPNS.Type
    Possible values: token (for tile notifications), toast or raw 可能的值: token (用于磁贴通知), toastraw

  2. Attribute name: AWS.SNS.MOBILE.MPNS.NotificationClass 属性名称: AWS.SNS.MOBILE.MPNS.NotificationClass
    Possible values: realtime *, priority , regular (realtime worked for me) 可能的值: realtime *, priorityregular (实时为我工作)


That said, it seems that it is not possible to send custom content to MPNS using the SNS console. 也就是说,似乎无法使用SNS控制台向MPNS发送自定义内容。 But using the API works, so here is an excerpt from the Java sample for using the Java SDK: 但是使用API​​工作,所以这里是Java示例中使用Java SDK的摘录:

AmazonSNS snsClient = ... /* initialise the client */;

Map<String, MessageAttributeValue> notificationAttributes = new HashMap<String, MessageAttributeValue>();
notificationAttributes.put("AWS.SNS.MOBILE.MPNS.Type",
        new MessageAttributeValue()
            .withDataType("String")
            .withStringValue("token")); // or "toast" or "raw", depending on the payload
notificationAttributes.put("AWS.SNS.MOBILE.MPNS.NotificationClass",
        new MessageAttributeValue()
            .withDataType("String")
            .withStringValue("realtime"));

PublishRequest request = new PublishRequest();
request.setMessageAttributes(notificationAttributes);
request.setMessageStructure("json");

request.setTargetArn(... /* topic or endpoint ARN */);
request.setMessage(... /* JSON payload */)

snsClient.publish(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM