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

╄→гoц情女王★ 提交于 2019-12-10 18:44:16

问题


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


回答1:


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 ){

    }

}



回答2:


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.




回答3:


Swift 4 solution:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
   if UIApplication.shared.applicationState == .background {
   //enter code here
   }
completionHandler()
}


来源:https://stackoverflow.com/questions/23009348/ios-how-can-i-tell-if-a-local-notification-caused-my-app-to-enter-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!