28

我是所有 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”标签。请告诉我如何实现?

4

3 回答 3

43

不允许将自定义标签放在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 文档中找到有关自定义有效负载的更多信息以及一些示例。

问候, HrisTo

于 2013-10-30T12:26:54.493 回答
8

您可以添加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是自定义有效负载,您可以在其中根据需要设置自己的数据

于 2019-09-15T19:08:04.027 回答
0

我正在使用推锐库。

 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));
        }
于 2019-07-10T11:40:57.517 回答