2015-09-04 62 views
1

我已爲我的警報項目創建了推送本地通知。實施接收推送通知操作的代理

問題是,當我點擊一個動作按鈕(貪睡和好吧)我實現的委託沒有被調用。

這裏是我的推本地通知代碼:

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

請檢查下面的代碼,它可以幫助你..

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協議2種新方法。

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

+0

我遵循的步驟,沒有錯誤,並運行,但仍然被點擊按鈕的動作時,不記錄。 – theFool