问题描述
有没有办法通过本地通知知道应用程序是否已激活?
Is there a way to know if the application did become active from a local notification?
我知道有一种方法可以测试应用程序是否从本地通知警报启动;但如果它只是坐在后台并收到通知?
I know there is a way to test if the application was launched from a local notification alert; but if it just was sitting out there the background, and received a notification?
当应用激活时,我需要运行不同的代码:
I need to run different code, when the app has become active:
来自本地通知.刚刚开始活跃:)有办法吗?
推荐答案
恐怕 Sylter 不正确.当应用程序从后台进入前台时,无论是通过直接用户操作还是通过用户对 UILocalNotification
的响应,它都不会触发 applicationDidFinishLaunchingWithOptions
.但是,它确实调用了 applicationWillEnterForeground
和 applicationDidBecomeActive
.这可以通过几个 NSLogs
来验证.
I'm afraid Sylter is incorrect. When an app enters the foreground from the background, either by a direct user action or by a user response to a UILocalNotification
, it does not trigger applicationDidFinishLaunchingWithOptions
. It does, however, call applicationWillEnterForeground
and applicationDidBecomeActive
. This can be verified with a couple of NSLogs
.
所以,问题依然存在:如果应用从后台进入前台,则无法发现应用是否响应用户对 UILocalNotification
的响应而进入前台,或者如果它只是进入前台.除了...
So, the problem remains: if an app is entering the foreground from the background, there is no way to discover if the app is entering the foreground in response to a user's response to a UILocalNotification
, or if it is merely entering the foreground. Except...
一旦应用进入前台,它会收到application:DidReceiveLocalNotification
方法:如果应用进入前台是为了响应UILocalNotification
.
Once the app has entered the foreground, it will receive the method application:DidReceiveLocalNotification
: if the app entered the foreground in response to a UILocalNotification
.
问题是在 application:DidReceiveLocalNotification:
方法中为响应接收 UILocalNotification 而进行的任何 UI 更改发生在 应用已经进入前台之后,从而创建了一个给用户带来不安的体验.
The problem is that any UI changes made within the application:DidReceiveLocalNotification:
method in response to receiving the UILocalNotification occur after the app has already entered the foreground, creating a disturbing experience for the user.
有人找到解决办法了吗?
Has anyone found a solution?