实现委托以接收推送通知操作

时间:2015-09-04 04:01:28

标签: ios objective-c uilocalnotification

我已为我的闹钟项目创建了推送本地通知。

问题是,当我点击一个动作按钮(贪睡并且没问题)时,我实施的代表不会被调用。

以下是推送本地通知的代码:

UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = Snooze;
notificationAction1.title = @"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;

UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = Okay;
notificationAction2.title = @"Okay";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = NO;
notificationAction2.authenticationRequired = NO;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Alarm";
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];

以下是实现委托的代码:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {    
    if ([identifier isEqualToString:Snooze]) {

        NSLog(@"You chose Snooze");
    }
    else if ([identifier isEqualToString:Okay]) {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        NSLog(@"You chose Okay");
    }
    if (completionHandler) {

        completionHandler();
    }
}

我还不知道如何使贪睡和好的按钮工作所以我只记录了一些东西,但是当我点击贪睡/好的时候什么也没有记录。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

请检查以下代码,它可能对您有所帮助..

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];
}

UIApplicationDelegate协议有两种新方法。

1) application:handleActionWithIdentifier:forLocalNotification:completionHandler:
2) application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

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

- (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();
    }
}

Source Link