1

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.

3 Answers3

4

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");
    }
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • 1
    Thanks akashg for instant reply. But this method doesn't called. Because the app is already launched. – Muhammad Zeeshan Mar 14 '14 at 07:16
  • Check for further clarity here: http://stackoverflow.com/questions/4136333/test-if-app-did-become-active-from-a-uilocalnotification – ZeMoon Mar 14 '14 at 07:19
  • The answer with the maximum vote-ups on that page should help. – ZeMoon Mar 14 '14 at 07:23
  • I've checked but when i debug didReceiveLocalNotification doesn't get called as i mentioned in question. – Muhammad Zeeshan Mar 14 '14 at 07:29
  • Is this happening in iOS 6 as well? – ZeMoon Mar 14 '14 at 07:31
  • I don't know. But i am using ios7. I am debugging using breakpoints. When the app is at background state and notification is raised didReceiveLocalNotification doesn't call. – Muhammad Zeeshan Mar 14 '14 at 07:37
  • You need to check whether you app is in active state or not. if its in active state then simply return otherwise perform some action. if ( application.applicationState == UIApplicationStateActive ) { return; } else { //perform action } – PunjabiCoder Mar 14 '14 at 07:47
  • Please add some code related to your implementation of UILocalNotification – ZeMoon Mar 14 '14 at 07:49
  • Bundle of Thanks. I got it – Muhammad Zeeshan Mar 14 '14 at 11:50
  • Muhammad: How did you resolve this? I'm experiencing the same thing that `didReceiveLocalNotification:` is not called after the application wakes from inactive state. – Markus Rautopuro Aug 28 '14 at 16:44
  • 1
    @MarkusRautopuro: see the first part of the answer. if app is killed and you tap on notification the '-applicationDidFinishLaunchingWithOptions' get called. otherwise 'didReceiveLocalNotification'. edited the answer. – nitish005 Apr 01 '16 at 13:04
2

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
}
svrushal
  • 1,612
  • 14
  • 25
1

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

Raj
  • 413
  • 1
  • 4
  • 5