动作按钮不会显示在通知中

时间:2015-01-26 17:21:16

标签: ios ios8 apple-push-notifications

我正在尝试将iOS8操作按钮添加到我已经正在处理的通知中。但他们不会出现。这只是没有按钮的正常通知。

//Action
UIMutableUserNotificationAction* loginAction = [[UIMutableUserNotificationAction alloc] init];
loginAction.identifier = @"loginAction"; //ID string to be passed back to your app when you handle the action
loginAction.title = @"login";
loginAction.activationMode = UIUserNotificationActivationModeForeground;
loginAction.destructive = NO;
loginAction.authenticationRequired = YES;

//Category
UIMutableUserNotificationCategory* loginCategory = [[UIMutableUserNotificationCategory alloc] init];
loginCategory.identifier = @"loginCategory"; //ID to include in your push payload
[loginCategory setActions:[NSArray arrayWithObjects:loginAction, nil] forContext:UIUserNotificationActionContextDefault];

//Register Settings and Notification
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:[NSSet setWithObject:loginCategory]]];
[[UIApplication sharedApplication] registerForRemoteNotifications];

使用此代码与将categories参数设置为nil具有相同的效果。

有效载荷如下所示:

'{"aps":{"alert":{"body":"text"},"category":"loginCategory"}}'

我刚刚将类别密钥添加到有效负载中。

我缺少什么?

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的例子,在Objective-C中,关于如何注册有2个动作的通知。

NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";

- (void)registerForNotification {

    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:@"Action 1"];
    [action1 setIdentifier:NotificationActionOneIdent];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:@"Action 2"];
    [action2 setIdentifier:NotificationActionTwoIdent];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2] 
                    forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types
                                                 categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

要发送此类通知,只需将类别添加到有效负载中。

"aps" : { 
    "alert"    : "Pull down to interact.",
    "category" : "ACTIONABLE"
}

当用户从推送通知中选择操作时,将在后台调用这些方法。

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

    if ([identifier isEqualToString:NotificationActionOneIdent]) {

        NSLog(@"You chose action 1.");
    }
    else if ([identifier isEqualToString:NotificationActionTwoIdent]) {   

        NSLog(@"You chose action 2.");
    }    
    if (completionHandler) {

        completionHandler();
    }
}

希望这有助于......干杯!!!