如何在iOS中定义推送通知的类型?

How to define types of push notification in iOS?

我想定义推送通知的类型。后面有不同的动作。

例如,接收类型A的选项卡A的徽章被更新,接收类型B的选项卡B的徽章被更新。

目前简单的payload如下。 {"aps":{"alert":"nickname001."}}

如何定义推送通知的类型?

只需在您的负载中添加一对键值对

例如:

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

然后当收到推送通知时,只需检查并按照您的要求进行操作即可。

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;

您也可以分别通过嵌套键和嵌套 switch-case 来实现……例如:

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

并分别

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;