c# - 具有自定义 iOS 负载的 Amazon Simple Notification Service 并不那么简单

标签 c# ios notifications amazon payload

发送纯文本通知很简单,而且有据可查。但我今天一直在纠结如何为 iOS 发送自定义通知,其中包含警报和 userId 等一些字段。

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

如果我将 MessageStructure 显式设置为 json,我会收到错误消息:“无效参数:消息结构 - JSON 消息正文无法解析”,但我很确定我的 JSON 是好的,当发送到 SNS 时,消息属性中的字符串看起来像这样:

{ "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 发送带有自定义负载的通知的好例子?因为亚马逊肯定没有……谢谢!

最佳答案

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

{"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();

关于c# - 具有自定义 iOS 负载的 Amazon Simple Notification Service 并不那么简单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959750/

相关文章:

c# - EntityType 没有在 MVC 4 中定义的键

c# - 为 MVC 中的特定 View 使用特定的 CSS

android: NotificationManager 无法解析为变量

swift - 在前台而不是后台接收 Firebase 通知

C# socket重构(数据发送)

c# - 如何用 Dictionary<string, Func<>> 替换这个 switch-case ?

ios - 如何在 Swift 中集成 Google reCAPTCHA

iOS - 应用程序在配置的 iPad 上崩溃,在模拟器中运行正常

ios - 2019年App Store中被拒绝的iOS应用程序可以删除吗?

javascript - 收到 document.body 错误。帮助!