gpt4 book ai didi

iOS 推送通知自定义格式

转载 作者:IT王子 更新时间:2023-10-29 08:02:54 25 4
gpt4 key购买 nike

我是所有 iOS 推送通知域的新手。我已经尝试使用以下代码进行基本的推送通知,并且效果很好。我正在使用“使用 JdSoft.Apple.Apns.Notifications;”来完成这个。这是代码:

Notification alertNotification = new Notification(testDeviceToken);

alertNotification.Payload.Alert.Body = "Hello World";
alertNotification.Payload.Sound = "default";
alertNotification.Payload.Badge = 1;

这会按照以下结构向 iPhone 提供输出:

{
aps = {
alert = "Hello World";
badge = 1;
sound = default;
};
}

我现在得到了添加自定义标签的要求,如下所示:

{
  "aps":   {
    "alert": "Hello World",
    "sound": "default",
"Person":     {
      "Address": "this is a test address",
      "Name": "First Name",
      "Number": "023232323233"
    
}  
}
}

我发现很难在“aps”中找到“Person”。我还知道您可以使用以下代码添加自定义属性:

alertNotification.Payload.AddCustom("人", Newtonsoft.Json.JsonConvert.SerializeObject(stat));

但是上面的代码并没有加上“aps”标签。请告诉我如何实现?

最佳答案

您不能将自定义标签放在aps 标签内。以下是文档中关于它的内容:

Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.

所以在你的情况下你应该做这样的事情:

{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}

因此,您可以通过在主 JSON 中查找它的键来读取您的自定义负载,而不是在“aps”中:

NSLog(@"%@",notification['Person']['Address']);

上面会输出:

this is a test address

您可以在 Apple docs 中找到更多关于自定义负载的信息以及一些示例.

问候,赫里斯托

关于iOS 推送通知自定义格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15834581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com