使用Push-Sharp库发送Json到APNS
本文关键字:Json APNS Push-Sharp 使用 | 更新日期: 2023-09-27 18:04:32
我需要像这样发送json到IOS设备:
{"aps":{"content-available":1}}
我应该使用AppleNotification还是AppleNotificationPayLoad类?请给出示例代码。下面是我现在如何创建通知的示例:
AppleNotification notification = NotificationFactory.Apple()
.ForDeviceToken(token)
.WithAlert(message)
.WithSound("default")
.WithBadge(7);
正如Eran所提到的,使用PushSharp,您只能在aps之外发送自定义有效负载参数。如果你想发送这样的东西:
{"aps":{ whatever data... },"content-available":1}
你可以这样做:
// Add these to your references
using System.IO;
using PushSharp;
using PushSharp.Apple;
using PushSharp.Core;
//Create our push services broker
var push = new PushBroker();
//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes(string.Format(@"applePushCert.p12"));
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "password"));
push.QueueNotification(new AppleNotification()
.ForDeviceToken("your device token")
.WithAlert("Hello World!")
.WithBadge(7)
.WithSound("sound.caf")
.WithCustomItem("content-available", 1));
您试图发送的JSON无效。您不能在aps
字典中放入自定义属性。如果你想发送自定义数据,你应该把它发送到aps
字典之外,像这样:
{"aps":{},"content-available":1}
你应该在PushSharp库中寻找允许你向有效负载添加自定义数据的方法。
提供商可以在apple保留的范围之外指定自定义有效负载值aps的名称空间。自定义值必须使用JSON结构和基本类型:字典(对象)、数组、字符串、数字和布尔。
编辑:
可以使用AppleNotificationPayload
的AddCustom(string key, params object[] values)
方法
这是一个旧线程,但我只是在2015年搜索它,找不到堆栈溢出的解决方案。我认为这个问题现在需要一个答案,因为要使用
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^(UIBackgroundFetchResult))completionHandler
方法时,必须包含"content-available" INSIDE。(详情请参阅这篇优秀的文章)
我发现pushsharp通过允许您在通知的末尾添加notification.WithContentAvailable(1)
来适应这一点,如下所示:
var notification = new AppleNotification(deviceID1, payload1);
notification.WithContentAvailable(1);//enable background fetch