【问题标题】:Send Json to APNS using Push-Sharp library使用 Push-Sharp 库将 Json 发送到 APNS
【发布时间】:2013-03-19 07:48:59
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# json push-notification apple-push-notifications pushsharp


    【解决方案1】:

    正如 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));
    

    【讨论】:

    • 在 iOS 和 didReceiveRemoteNotification 方法中获取这个自定义项目。使用这个: NSString *contentId = [userInfo objectForKey:@"content-available"];
    【解决方案2】:

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

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

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

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

    编辑:

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

    【讨论】:

      【解决方案3】:

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

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

      方法,你必须在aps中包含“content-available”INSIDE。 (see this excellent article for details)

      我发现 pushsharp 可以解决这个问题,让您可以在通知末尾添加 notification.WithContentAvailable(1),如下所示:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        相关资源
        最近更新 更多