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