Frandro Frandro - 3 months ago 11
iOS Question

How to define types of push notification in iOS?

I'd like to define types of push notifications. Different actions are followed.

For example, the badge of tab A on receiving type A is updated and that of tab B on receiving type B is updated.

The current simple payload is as follows.
{"aps":{"alert":"nickname001."}}

How to define types of push notification?

Answer

Just add one key value pair in your payload

For Example :

{
    "aps": {
        "alert": "nickname001 just liked you!",
        "badge": 2
    },
    "action": 1
}

Then when receive push notification, just check and follow your requirement.

NSInteger page = [[userInfo objectForKey:@"action"] integerValue];
switch (page) {

    case 1:
        {
            //Update tab A - type A
        }
        break;

    case 2:
        {
            //Update tab B - type B
        }
        break;

    default:
        break;

Also you can do it by nested keys and nested switch-case respectively…like :

{
    "aps": {
        "alert": "nickname001 just liked you!",
        "badge": 2
    },
    "action": {
        "tab": 1,
        "type": "A"
    }         
}

and respectively

NSInteger page = [[[userInfo objectForKey:@“action”] objectForKey:@“tab”] integerValue];
switch (page) {

    case 1:
            {
                NSString *strType = [[[userInfo objectForKey:@"action"] objectForKey:@"type"] uppercaseString];
                if ([strType isEqualToString:@"A"]) {

                    // update type A
                }
                else {

                   // update type B
                }
        }
        break;

    case 2:
            {
    }
        break;

default:
        break;