58

I am sending push notifications from php job application to iphone. I am sending push notifications regarding new jobs. Is this possible that when user click on the view of push notification pop up , then user redirect to the particular job in the device.

I mean I wanted to know can I send any custom data with push notification like jobId,something else....so that Iphone end Can retrieve and show the particular job ?

Thanks.

2
  • 3
    oh..sorry rckones..I didnt know how to accpt as answer..my fault..now sorted it out... :)
    – vikas
    Nov 15, 2011 at 9:58
  • 6
    We are all here to learn.
    – rckoenes
    Nov 15, 2011 at 10:02

3 Answers 3

125

Regardless of the language and library you use, the push notification payload is a JSON payload:

{
    "aps": {
         "badge": 10,
         "alert": "Hello world!",
         "sound": "cat.caf"
    }
}

The aps token is the Apple APN data. You can add custom data to your payload as well:

{
    "aps": {
         "badge": 10,
         "alert": "Hello world!",
         "sound": "cat.caf"
    },
    "job_id": 1
}

When you receive the notification in the app, check for your param in the notification dictionary:

- (void)handleBackgroundNotification:(NSDictionary *)notification
{
    NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
    NSMutableString *alert = [NSMutableString stringWithString:@""];
    if ([aps objectForKey:@"alert"])
    {
        [alert appendString:(NSString *)[aps objectForKey:@"alert"]];
    }
    if ([notification objectForKey:@"job_id"])
    {
        // do something with job id
        int jobID = [[notification objectForKey:@"job_id"] intValue];
    }
}

Keep in mind that the total size of the payload is 256 bytes, and that includes, of course, your custom parameters. So you may have to (at risk of reducing readability) call your custom param "ji" instead of "job_id" to squeeze bytes.

All of this is documented in the Local and Push Notification Programming Guide in the iOS documentation. Definitely would recommend a read because it's more complex than it initially sounds (at least, that's what I thought).

4
  • How can I add custom data to the payload?
    – Haring10
    Oct 31, 2014 at 15:27
  • @JoshHarington "job_id": 1 is a custom data in the payload Mar 8, 2015 at 13:31
  • 12
    From iOS 8 the payload size has been increased to 2KB from 256 bytes :) Mar 25, 2015 at 18:11
  • Are you getting custom data payload in iOS 10? Mar 10, 2017 at 13:27
8

Yes you can send custom data, check apns-php library for all push notification needs:

2
  • Hey : Will you please explain format of Push in Php.
    – Asim Khan
    Dec 11, 2017 at 8:12
  • @Asim I don't have much info on format, but you can use this library i mentioned, for PHP. You can reverse engineer to see the format.
    – Arfeen
    Dec 11, 2017 at 8:31
-1

We can add more than one custom data,but, if i use sub_action as the name, my iPhone can't receive push msg

Java:

PayLoad payLoad = new PayLoad();
payLoad.addCustomDictionary("action", action_type);
payLoad.addCustomDictionary("subaction", sub_action_type);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.