27
votes

I'm new to all iOS push notification domain. I have tried a basic push notification using the following code and it works perfectly. I'm using "using JdSoft.Apple.Apns.Notifications;" to accomplish this. Here's the code:

Notification alertNotification = new Notification(testDeviceToken);

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

This gives the output to the iPhone in the following structure:

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

I have now got the requirement to add a custom tag as follows:

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

I find it difficult to get "Person" inside "aps". I also know that you can add a custom attribute using the following code:

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

But the above code does not add withing "aps" tag. Please tell me how it can be achieved?

3
Custom entities should not be in the APS element. Apples example payloadrckoenes
Refer this for notification payload format stackoverflow.com/a/57685923/11228616Siddhesh Bhide

3 Answers

43
votes

You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:

Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.

So in your case you should do something like:

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

Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

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

Above will output:

this is a test address

You could find more about custom payloads, along with some examples in Apple docs.

Regards, HrisTo

6
votes

You can add Title, Subtitle, body and many other keys as

{
  "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"
  }
} 

Where Employee is custom payload where you can set your own data as required

0
votes

I am using 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));
        }