iOS - How can I tell if a Local Notification caused my app to enter foreground?
Asked Answered
S

3

3

I have code within my app delegate's

    application:didReceiveLocalNotification:

method to display an UIAlertView for that local notification, whenever my app is in the foreground.

If my app is within the background when the local notification arrives the user gets presented with the notification and is able to launch the app by selecting it. In this case my app comes to the foreground my App Delegate's

    applicationWillEnterForeground:

is called. Afterwards though my didReceiveLocalNotification method is called again, causing an UIAlertView to appear again. But really the user has already had that alert whilst the app was in the background, so ideally I'd like to not display this alert again.

I can see that if an app is launched due to a local notification then within the

    application:didFinishLaunchingWithOptions:

method you can inspect the launch options for a key

    UIApplicationLaunchOptionsLocalNotificationKey

to know whether or not a local notification caused your app to launch, but there seems to be no such method for finding this out when you are just brought back into the foreground by the user interacting with a local notification.

Checking whether or not my applicationWillEnterForeground method has been called recently would seem a hacky way around this problem, or perhaps something similar to the answers given in this question "iOS how to judge application is running foreground or background?" will allow me to check the

    [UIApplication sharedApplication].applicationState

from within my

    application:didReceiveLocalNotification:

method. Hopefully it'll be received early enough that my applicationState will still not be set to UIApplicationStateActive in this case.

Or are there any better solutions for this?

Cheers

Spinous answered 11/4, 2014 at 9:56 Comment(2)
Check out this: #4136833Spew
Thanks. I'll try the fix later.. Cheers.Spinous
D
6

in AppDelegate you can check state of app when application receives Notification

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
     // check state here 

  if(state ==UIApplicationStateBackground ){

    }

}
Deadening answered 11/4, 2014 at 10:7 Comment(1)
Thanks. I'll try this later.. it's all coming together, slowly but surely. CheersSpinous
S
0

Just wanted to say that I just noticed the suggested answer of checking on the applicationState has a bit of a bad side effect that it will stop anything happening whilst notification centre is open and over the top of your app. Personally I didn't want this to stop my alert views from being created so i came up with an alternative.

Basically I just record the date when my app was last launched or foregrounded and then whenever testing my notification dates I compare their fireDate with the appLastStarted date and only display the notification if it's occurred since my app's been in the foreground. This fixes the problem with opening the app from a notification, but also allows the alerts to show when the app is not active (ie. behind notification centre).

I've yet to experience any issues with this approach, though admittedly I've only been trying it from today so it's not had a great deal of testing.

Just thought I'd record it unless anyone else had similar requirements.

Spinous answered 30/9, 2014 at 21:17 Comment(1)
You can also use UIApplicationStateInactive and UIApplicationStateActive to check whether notification center is open or not. Only when application is open and running, the state will be active, but if notification center is open, state will be inactive.Emmalynn
I
0

Swift 4 solution:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
   if UIApplication.shared.applicationState == .background {
   //enter code here
   }
completionHandler()
}
Incontrovertible answered 22/6, 2018 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.