【问题标题】:iOS Push Notification custom formatiOS 推送通知自定义格式
【发布时间】:2013-03-27 21:46:11
【问题描述】:

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

Notification alertNotification = new Notification(testDeviceToken);

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

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

{
    aps =     {
        alert = "Hello World";
        badge = 1;
        sound = default;
    };
}

我现在有添加自定义标签的需求,如下:

{
    "aps":   {
        "alert": "Hello World",
        "sound": "default",
    "Person":     {
            "Address": "this is a test address",
            "Name": "First Name",
            "Number": "023232323233"
          
    }  
  }
}

我发现很难在“aps”中找到“Person”。我也知道您可以使用以下代码添加自定义属性:

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

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

【问题讨论】:

标签: ios push-notification


【解决方案1】:

您不能将自定义标签放在 aps 标签内。以下是相关文档的说明:

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

因此,在您的情况下,您应该执行以下操作:

{
    "aps": {
        "alert": "Hello World",
        "sound": "default"
    },
    "Person": {
        "Address": "this is a test address",
        "Name": "First Name",
        "Number": "023232323233"
    }
}

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

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

上面会输出:

这是一个测试地址

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

问候, HrisTo

【讨论】:

    【解决方案2】:

    您可以添加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"
      }
    } 
    

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

    【讨论】:

      【解决方案3】:

      我正在使用 push sharp 库。

       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));
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-14
        相关资源
        最近更新 更多