Search code examples
objective-cxcodeuilocalnotification

Interactive notification does not open app after handling it's action


I have created an interactive notification with two actions : snooze and dismiss. Notification fires and I got the two actions. On tapping of it, it calls the method :

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

but it after completion, the app does not open. I want to open a viewcontroller on tap of snooze button but it neither calls didFinishLaunchingWithOptions nor didReceiveLocalNotification. Please guide me. Tutorials just having the logs that this action has been performed. stuck badly.

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

In this commented code I am trying to open a controller which never opens.


Solution

  • Please set setActivationMode to UIUserNotificationActivationModeForeground

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

    UIUserNotificationActivationModeForeground - activates the application in the foreground

    UIUserNotificationActivationModeBackground - activates the application in the background, unless it's already in the foreground