If application is in background state and launch on locanotification tap Which methods called in appdelegate

StackOverflow https://stackoverflow.com/questions/22398321

문제

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.

도움이 되었습니까?

해결책

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");
    }
}

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top