iOS推送通知自定义格式 [英] iOS Push Notification custom format
问题描述
我是所有iOS推送通知域的新用户。我已经尝试使用以下代码进行基本推送通知,它完美无缺。我正在使用使用JdSoft.Apple.Apns.Notifications;实现这一目标。这是代码:
I'm new to all iOS push notification domain. I have tried a basic push notification using the following code and it works perfectly. I'm using "using JdSoft.Apple.Apns.Notifications;" to accomplish this. Here's the code:
Notification alertNotification = new Notification(testDeviceToken);
alertNotification.Payload.Alert.Body = "Hello World";
alertNotification.Payload.Sound = "default";
alertNotification.Payload.Badge = 1;
这将以下列结构输出到iPhone:
This gives the output to the iPhone in the following structure:
{
aps = {
alert = "Hello World";
badge = 1;
sound = default;
};
}
我现在需要添加自定义标签,如下所示:
I have now got the requirement to add a custom tag as follows:
{
"aps": {
"alert": "Hello World",
"sound": "default",
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}
}
我发现很难在aps中获得人物。我还知道您可以使用以下代码添加自定义属性:
I find it difficult to get "Person" inside "aps". I also know that you can add a custom attribute using the following code:
alertNotification.Payload.AddCustom(Person,Newtonsoft.Json.JsonConvert.SerializeObject(stat) );
alertNotification.Payload.AddCustom("Person", Newtonsoft.Json.JsonConvert.SerializeObject(stat));
但上面的代码没有添加aps标签。请告诉我如何实现?
But the above code does not add withing "aps" tag. Please tell me how it can be achieved?
推荐答案
您不能在 aps 标记内放置自定义标记。以下是有关文档的说明:
You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
提供者可以在Apple保留的aps命名空间之外指定自定义有效负载值。自定义值必须使用JSON结构化和原始类型:字典(对象),数组,字符串,数字和布尔值。
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.
所以在你的情况下你应该这样做:
So in your case you should do something like:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "this is a test address",
"Name": "First Name",
"Number": "023232323233"
}
}
因此,您可以通过查找主JSON中的密钥来读取自定义有效负载,而不是aps:
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":
NSLog(@"%@",notification['Person']['Address']);
以上将输出:
这是一个测试地址
this is a test address
您可以找到更多关于自定义有效负载的信息,以及 Apple docs 。
You could find more about custom payloads, along with some examples in Apple docs.
问候,
HrisTo
Regards, HrisTo
这篇关于iOS推送通知自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!