繁体   English   中英

iOS推送通知自定义格式

[英]iOS Push Notification custom format

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

您不能在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

您可以添加标题副标题正文和许多其他键

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

我正在使用push sharp library。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM