iOS - 如何判断本地通知是否导致我的应用进入前台? [英] iOS - How can I tell if a Local Notification caused my app to enter foreground?

查看:27
本文介绍了iOS - 如何判断本地通知是否导致我的应用进入前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序委托中有代码

I have code within my app delegate's

    application:didReceiveLocalNotification:

每当我的应用处于前台时,为该本地通知显示 UIAlertView 的方法.

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:

被调用.之后虽然我的 didReceiveLocalNotification 方法再次被调用,导致 UIAlertView 再次出现.但实际上,当应用程序在后台时,用户已经收到了该警报,因此理想情况下,我不想再次显示此警报.

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.

检查最近是否调用了我的 applicationWillEnterForeground 方法似乎是解决此问题的一种hacky 方法,或者可能类似于此问题中给出的答案​​iOS 如何判断应用程序是在前台运行还是后台运行?" 将允许我检查

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

来自我的

    application:didReceiveLocalNotification:

方法.希望它能够尽早收到,在这种情况下,我的 applicationState 仍然不会设置为 UIApplicationStateActive.

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?

干杯

推荐答案

在 AppDelegate 中,您可以在应用程序收到通知时检查应用程序的状态

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

    }

}

这篇关于iOS - 如何判断本地通知是否导致我的应用进入前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆