C# 使用Push Sharp库向APNS发送Json

C# 使用Push Sharp库向APNS发送Json,c#,json,push-notification,apple-push-notifications,pushsharp,C#,Json,Push Notification,Apple Push Notifications,Pushsharp,我需要向IOS设备发送json,如下所示: {"aps":{"content-available":1}} {"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

我需要向IOS设备发送json,如下所示:

    {"aps":{"content-available":1}}
{"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));
我应该使用AppleNotification还是AppleNotificationPayLoad类?请给出示例代码。下面是我现在创建通知的示例:

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

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

{“aps”:{},“可用内容”:1}

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

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

编辑:


您可以使用Eran提到的
AppleNotificationPayload

AddCustom(字符串键,参数对象[]值)
方法,使用PushSharp,您只能在aps之外发送自定义负载参数。所以如果你想发送这样的东西:

    {"aps":{"content-available":1}}
{"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));
您可以这样做:

    {"aps":{"content-available":1}}
{"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));

这是一个旧线程,但我只是在2015年搜索了它,没有找到关于堆栈溢出的解决方案。我相信这个问题现在应该得到一个答案,即使用

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^(UIBackgroundFetchResult))completionHandler 
方法,则必须在aps中包含“可用内容”

我发现pushsharp通过使您能够在通知末尾添加
通知来适应这一点

 var notification = new AppleNotification(deviceID1, payload1);
 notification.WithContentAvailable(1);//enable background fetch

要在iOS和DidReceiveEmotentification方法中拾取此自定义项。使用以下内容:NSString*contentId=[userInfo objectForKey:@“内容可用”];