gpt4 book ai didi

iphone - 测试应用程序是否确实从 UILocalNotification 变为事件状态

转载 作者:IT老高 更新时间:2023-10-28 11:39:57 27 4
gpt4 key购买 nike

有没有办法通过本地通知知道应用程序是否已激活?

我知道有一种方法可以测试应用程序是否从本地通知警报启动;但如果它只是坐在后台并收到通知?

当应用激活时,我需要运行不同的代码:

  1. 来自本地通知。
  2. 刚刚开始活跃:)

有办法吗?

最佳答案

我从@naveed 关于在调用 didReceiveNotification 方法时检查应用程序状态的提示中得到了解决方案的线索。当应用从后台恢复时,无需检查变量等。

在 iOS7 及更低版本上,您可以这样处理通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (application.applicationState == UIApplicationStateInactive ) {
//The application received the notification from an inactive state, i.e. the user tapped the "View" button for the alert.
//If the visible view controller in your view controller stack isn't the one you need then show the right one.
}

if(application.applicationState == UIApplicationStateActive ) {
//The application received a notification in the active state, so you can display an alert view or do something appropriate.
}
}

iOS 8 更新:现在通过通知从后台打开应用程序时会调用以下方法。

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
}

如果在应用处于前台时收到通知,请使用以下方法:

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *) userInfo {
}

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}

请注意,除非您想在应用中支持旧版本的操作系统,否则无需检查应用状态。

关于iphone - 测试应用程序是否确实从 UILocalNotification 变为事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4136333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com