2016-07-22 85 views
0

我創建了一個包含兩個操作的交互式通知:貪睡和解除。 通知火災,我得到了兩個動作。在攻絲時,它調用方法:交互式通知在處理它的操作後未打開應用程序

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 

但它完成後,應用程序無法打開。我想打開一個視圖控制器點擊貪睡按鈕,但它既不會調用didFinishLaunchingWithOptions也不didReceiveLocalNotification。請指導我。教程只是具有執行此操作的日誌。嚴重卡住。

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 
{ 
    recentLocalnotification = notification; 
    application.applicationIconBadgeNumber = 0; 

    if ([identifier isEqualToString:kNotificationActionSnooze]) { 

     strTappedButtonFromNotification = kNotificationActionSnooze ; 
     [self GetTopViewController]; 

    //  UIStoryboard *storyboard = [CommonMethods getiPhoneOriPadStoryboard]; 
    //  UINavigationController *navController = (UINavigationController *)self.window.rootViewController; 
    //   
    //  SnoozeViewController *snooze = [storyboard instantiateViewControllerWithIdentifier:kSnoozeViewController]; 
    //  snooze.dictNotificationData = recentLocalnotification.userInfo; 
    //  [navController pushViewController:snooze animated:YES]; 
    //   
    //  [self applicationDidBecomeActive:[UIApplication sharedApplication]]; 
    } 
    else if ([identifier isEqualToString:kNotificationActionDismiss]) { 

     strTappedButtonFromNotification = kNotificationActionDismiss; 
     NSLog(@"Notification Dismissed"); 
    } 
    else{ 
     strTappedButtonFromNotification = @""; 
    } 

    completionHandler(); 
} 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    [[UIApplication sharedApplication]cancelAllLocalNotifications]; 

    application.applicationIconBadgeNumber = 0; 
    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (locationNotification) { 
     application.applicationIconBadgeNumber = 0; 
    } 

    UIMutableUserNotificationAction *action1; 
    action1 = [[UIMutableUserNotificationAction alloc] init]; 
    [action1 setActivationMode:UIUserNotificationActivationModeBackground]; 
    [action1 setTitle:@"SNOOZE"]; 
    [action1 setIdentifier:kNotificationActionSnooze]; 
    [action1 setDestructive:NO]; 
    [action1 setAuthenticationRequired:NO]; 

    UIMutableUserNotificationAction *action2; 
    action2 = [[UIMutableUserNotificationAction alloc] init]; 
    [action2 setActivationMode:UIUserNotificationActivationModeBackground]; 
    [action2 setTitle:@"DISMISS"]; 
    [action2 setIdentifier:kNotificationActionDismiss]; 
    [action2 setDestructive:NO]; 
    [action2 setAuthenticationRequired:NO]; 

    UIMutableUserNotificationCategory *actionCategory; 
    actionCategory = [[UIMutableUserNotificationCategory alloc] init]; 
    [actionCategory setIdentifier:kNotificationCategoryIdent]; 
    [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]; 

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; 

    return YES; 
} 

在這個評論代碼中,我試圖打開一個從不打開的控制器。

+0

你的應用程序在後臺或前景?你能告訴我如何使用UIMutableUserNotificationAction。 –

+0

應用程序在後臺 –

+0

UIMutableUserNotificationAction代碼? –

回答

2

請設置setActivationModeUIUserNotificationActivationModeForeground

UIMutableUserNotificationAction *action1; 
    action1 = [[UIMutableUserNotificationAction alloc] init]; 
    [action1 setActivationMode:UIUserNotificationActivationModeForeground]; 
    [action1 setTitle:@"SNOOZE"]; 
    [action1 setIdentifier:kNotificationActionSnooze]; 
    [action1 setDestructive:NO]; 
    [action1 setAuthenticationRequired:NO]; 

UIUserNotificationActivationModeForeground - 激活前景

UIUserNotificationActivationModeBackground應用 - 激活後臺應用程序,除非它已經在前臺

+0

wohooo ..這工作!非常感謝你:) –

+0

很棒...................... :) –

+0

我可以有兩個以上的動作嗎? –

相關問題