iOS 推送通知自定义格式 [英] iOS Push Notification custom format

查看:68
本文介绍了iOS 推送通知自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是所有 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"中找到Person".我也知道您可以使用以下代码添加自定义属性:

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']);

上面会输出:

这是一个测试地址

您可以在 Apple 文档.

问候,快点

这篇关于iOS 推送通知自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆