iOS 推送通知自定义格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15834581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:03:16  来源:igfitidea点击:

iOS Push Notification custom format

iospush-notification

提问by Pavan Welihinda

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:

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

Notification alertNotification = new Notification(testDeviceToken);

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

This gives the output to the iPhone in the following structure:

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

{
    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"
      ????
    }??
  }
}

I find it difficult to get "Person" inside "aps". I also know that you can add a custom attribute using the following code:

我发现很难将“人”放入“aps”中。我也知道您可以使用以下代码添加自定义属性:

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

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

But the above code does not add withing "aps" tag. Please tell me how it can be achieved?

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

回答by hris.to

You are not allowed to put custom tags inside apstag. Here's what documentations says about it:

不允许在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.

提供商可以在 Apple 保留的 aps 命名空间之外指定自定义负载值。自定义值必须使用 JSON 结构化和原始类型:字典(对象)、数组、字符串、数字和布尔值。

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"
    }
}

Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

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

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

Above will output:

以上将输出:

this is a test address

这是一个测试地址

You could find more about custom payloads, along with some examples in Apple docs.

您可以在Apple 文档中找到更多关于自定义有效负载的信息以及一些示例。

Regards, HrisTo

问候, HrisTo

回答by Siddhesh Bhide

You can add Title, Subtitle, bodyand many other keys as

您可以添加TitleSubtitlebody和许多其他键作为

{
  "aps": {
    "alert": {
      "title": "Hey! Checkout my custom notification",
      "subtitle": "Custom notification subtitle",
      "body": "Description of custom notification"
    },
    "sound": "default",
    "category": "CustomPush",
    "badge": 1,
    "mutable-content": 1
  },
  "Employee": {
    "Name": "John Doe",
    "Designation": "Manager"
  }
} 

Where Employeeis custom payload where you can set your own data as required

其中Employee是自定义有效负载,您可以根据需要在其中设置自己的数据

回答by Siddhesh Bhide

I am using push sharp library.

我正在使用推锐库。

 public static JObject CreatePayload(APNSNotification apnsNotification, object content, int Ntype)
        {
            var payload = new Dictionary<string, object>();
            var aps = new Dictionary<string, object>();


            if ((int)NotificationType.CONFERENCE == Ntype)
            {
                var confNotification = new ConferenceNotification();
                confNotification = (ConferenceNotification)content;

                aps.Add("alert", confNotification.title);
                aps.Add("subtitle", confNotification.body);
                aps.Add("badge", confNotification.badgeCount);

                payload.Add("aps", aps);


                payload.Add("confId", confNotification.confId);
                payload.Add("pageFormat", confNotification.pageFormat);
                payload.Add("pageTitle", confNotification.pageTitle);
                payload.Add("webviewURL", confNotification.webview_URL);
                payload.Add("notificationBlastID", confNotification.notificationBlastID);
                payload.Add("dataValue", confNotification.dataValue);
                payload.Add("pushtype", "Events");
            }
            else if ((int)NotificationType.NEWS == Ntype)
            {
                var newsNotification = new NewsNotification();
                newsNotification = (NewsNotification)content;

                aps.Add("alert", newsNotification.title);
                aps.Add("subtitle", newsNotification.subtitle);
                aps.Add("badge", newsNotification.badgeCount);

                payload.Add("aps", aps);

                payload.Add("articleId", newsNotification.articleId);
                payload.Add("msgcnt", newsNotification.msgcnt);
                payload.Add("type", newsNotification.type);
                payload.Add("pushtype", "News");
            }

            return JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(payload));
        }