2

我需要像这样将json发送到IOS设备:

    {"aps":{"content-available":1}}

我应该使用 AppleNotification 还是 AppleNotificationPayLoad 类?请提供示例代码。这是我现在如何创建通知的示例:

        AppleNotification notification = NotificationFactory.Apple()
                                                      .ForDeviceToken(token)
                                                      .WithAlert(message)
                                                      .WithSound("default")
                                                      .WithBadge(7);
4

3 回答 3

4

正如 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));
于 2013-09-05T23:38:02.467 回答
2

您尝试发送的 JSON 无效。您不能将自定义属性放入aps字典中。如果要发送自定义数据,则应将其发送到aps字典之外,如下所示:

{"aps":{},"content-available":1}

您应该在 PushSharp 库中查找允许您将自定义数据添加到有效负载的方法。

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

编辑 :

可以使用的AddCustom(string key, params object[] values)方法AppleNotificationPayload

于 2013-03-19T10:27:12.560 回答
0

这是一个旧线程,但我只是在 2015 年搜索它,找不到堆栈溢出的解决方案。我相信这个问题现在值得回答,因为使用

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^(UIBackgroundFetchResult))completionHandler 

方法,您必须在aps中包含“内容可用” 。(详见这篇优秀的文章)

我发现 pushsharp 通过使您能够notification.WithContentAvailable(1)像这样添加通知的结尾来适应这一点:

 var notification = new AppleNotification(deviceID1, payload1);
 notification.WithContentAvailable(1);//enable background fetch
于 2015-05-19T11:26:35.150 回答