Question

In my app I am using uilocalnotifications. Every thing is ok but one thing. I need to show notifications's alertbody. If app is in foreground state it's fine, but if app is at background state and notification occurs, when i tap on that didReceiveLocalNotification doesn't get called. Obviously didFinishLaunchingWithOptions is also don't called at that time. So what should i do to handle the notification. I am using ios7 and xcode5. Thanks very much in advance if you could help me.

Was it helpful?

Solution

For an app which is not in the foreground, the local notification can subsequently be found in the

-applicationDidFinishLaunchingWithOptions method

UILocalNotification *localNotif =
        [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {
        //Handle local notification here.
    }

You can read Apple's documentation for handling notifications here.

If the app is currently in memory, you can check it's state in the following way:

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    if (app.applicationState == UIApplicationStateInactive )
    {
        NSLog(@"app not running");
    }
    else if(app.applicationState == UIApplicationStateActive )
    {
        NSLog(@"app running");
    }
}

OTHER TIPS

if application is closed and notification is raised then for that you have to write below code in appdidfinishlaunching method

// Handle launching from a notification
    UILocalNotification *objLocalNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (objLocalNotif)
    {
        NSLog(@"At the time of launching Recieved Notification %@",objLocalNotif);
        //Do your stuff here
    } 

If application is in background and when any local notification is raised the following method of app delegate get called.

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    // Handle the notificaton when the app is running

    NSLog(@"Recieved Notification %@",notif);
    //do your stuff here
}

Write your code in the below method

- (void)applicationWillEnterForeground:(UIApplication *)application

it will be called when you open your application again which has not been terminated fully but still running in background

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top