簡體   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