Amazon Simple Notification Service与自定义iOS有效负载并不那么简单(Amazon Simple Notification Service with custom i

编程入门 行业动态 更新时间:2024-10-28 03:18:20
Amazon Simple Notification Service与自定义iOS有效负载并不那么简单(Amazon Simple Notification Service with custom iOS payload not so simple)

发送纯文本通知非常简单并且有据可查。 但我今天一直在拉我的头发,发送一个iOS自定义通知,其中包含警报和一些像userId这样的字段。

我从这个帮助页面开始实现类似于最后一个示例,然后我发现这个答案似乎使帮助页面上的最后一个示例失效,因为“url”属性应该在“aps”对象之外。 我尝试了很多组合,但每个组合都以文本形式发送到应用程序(整个消息,带有“默认”属性和“APNS”对象)...

如果我明确地设置MessageStructure为JSON,我得到的错误:“无效的参数:消息结构 - JSON邮件正文解析失败”,但我很确定我的JSON是好的,当发送到SNS在Message属性中的字符串看起来像这样:

{ "default":"You received a new message from X.", "APNS_SANDBOX":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, \"event\":\"Message\", \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\" }", "APNS":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, \"event\":\"Message\", \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\" }" }

有没有人有一个很好的例子,通过C#中的SNS发送带有自定义有效载荷的通知? 因为亚马逊肯定没有......谢谢!

Sending a plain text notification is easy and well documented. But I've been pulling my hair today regarding sending a custom notification for iOS that has the alert and some fields like userId.

I started with this help page and implemented something similar to the last sample, then I found this answer that seems to invalidate the last sample on the help page, as the "url" property should be outside the "aps" object. I tried a good deal of combinations but each one of them gets sent as text to the app (the whole message, with the "default" property and "APNS" object)...

If I explicitly set MessageStructure to json I get the error: "Invalid parameter: Message Structure - JSON message body failed to parse" but I'm pretty sure my JSON is good, when sent to SNS the string in the Message property looks like this:

{ "default":"You received a new message from X.", "APNS_SANDBOX":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, \"event\":\"Message\", \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\" }", "APNS":"{ \"aps\": {\"alert\":\"You received a new message from X.\"}, \"event\":\"Message\", \"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\" }" }

Does anybody have a good example of sending a notification with custom payload through SNS in C#? Because Amazon sure hasn't... Thank you!

最满意答案

奇怪的是,当我通过使用类和序列化对象而不是仅仅发送一个格式化的字符串来实现干净的方式时, 唯一的区别是间距...在干净版本中除了属性值之外没有空格:

{"default":"You received a new message from X.","APNS_SANDBOX":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}","APNS":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}"}

这些是我正在序列化的类(仅适用于APNS),使用您需要的任何属性而不是Event和ObjectID:

[DataContract] public class AmazonSNSMessage { [DataMember(Name = "default")] public string Default { get; set; } [DataMember(Name = "APNS_SANDBOX")] public string APNSSandbox { get; set; } [DataMember(Name = "APNS")] public string APNSLive { get; set; } public AmazonSNSMessage(string notificationText, NotificationEvent notificationEvent, string objectID) { Default = notificationText; var apnsSerialized = JsonConvert.SerializeObject(new APNS { APS = new APS { Alert = notificationText }, Event = Enum.GetName(typeof(NotificationEvent), notificationEvent), ObjectID = objectID }); APNSLive = APNSSandbox = apnsSerialized; } public string SerializeToJSON() { return JsonConvert.SerializeObject(this); } } [DataContract] public class APNS { [DataMember(Name = "aps")] public APS APS { get; set; } [DataMember(Name = "event")] public string Event { get; set; } [DataMember(Name = "objectID")] public string ObjectID { get; set; } } [DataContract] public class APS { [DataMember(Name = "alert")] public string Alert { get; set; } }

所以我通过这样做得到了Amazon SNS消息:

new AmazonSNSMessage(...).SerializeToJSON();

Strangely when I implemented the clean way of doing this by using classes and serializing objects instead of just sending a formatted string it worked. The only difference was the spacing... in the clean version there are no spaces except in the property values:

{"default":"You received a new message from X.","APNS_SANDBOX":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}","APNS":"{\"aps\":{\"alert\":\"You received a new message from X.\"},\"event\":\"Message\",\"objectID\":\"7a39d9f4-2c3f-43d5-97e0-914c4a117cee\"}"}

These are the classes that I'm serializing (only for APNS for the moment), use whatever properties you need instead of Event and ObjectID:

[DataContract] public class AmazonSNSMessage { [DataMember(Name = "default")] public string Default { get; set; } [DataMember(Name = "APNS_SANDBOX")] public string APNSSandbox { get; set; } [DataMember(Name = "APNS")] public string APNSLive { get; set; } public AmazonSNSMessage(string notificationText, NotificationEvent notificationEvent, string objectID) { Default = notificationText; var apnsSerialized = JsonConvert.SerializeObject(new APNS { APS = new APS { Alert = notificationText }, Event = Enum.GetName(typeof(NotificationEvent), notificationEvent), ObjectID = objectID }); APNSLive = APNSSandbox = apnsSerialized; } public string SerializeToJSON() { return JsonConvert.SerializeObject(this); } } [DataContract] public class APNS { [DataMember(Name = "aps")] public APS APS { get; set; } [DataMember(Name = "event")] public string Event { get; set; } [DataMember(Name = "objectID")] public string ObjectID { get; set; } } [DataContract] public class APS { [DataMember(Name = "alert")] public string Alert { get; set; } }

So I get the Amazon SNS message by doing:

new AmazonSNSMessage(...).SerializeToJSON();

更多推荐

本文发布于:2023-07-17 04:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139276.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   负载   简单   Service   Amazon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!