Search code examples
iosobjective-cuilocalnotificationalarm

how to handle NSLocal Notification in background?


  • For multiple Alarm Notification Alert?

  • If Application is Running in Background.


Solution

  • - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif{
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
        if (application.applicationState == UIApplicationStateInactive )
        {
            NSLog(@"app not running");
        }
        else if(application.applicationState == UIApplicationStateActive )
        {
            NSLog(@"App is running");
    
        }else if(application.applicationState == UIApplicationStateBackground){
            NSLog(@"App is running in background");
        }
    }
    

    In Addition to JAGAT's answer for iOS 8+ you need to ask for permission to register for listening to local notifications. And you can user this:

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

    And for permisson do this:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                             categories:nil]];
    }