确定 UILocalNotification 是在前台还是后台由应用程序触发
接收 UILocalNotification
时
- 当应用程序处于前台时,会调用
application:DidReceiveLocalNotification
方法。 - 当应用程序处于后台时,它会打开应用程序,然后调用
application:DidReceiveLocalNotification
。
我如何确定正在发生的是哪一种情况?我想以不同的方式对待他们。
如果应用程序位于后台,则用户表示他们想要查看该事件。所以在 application:DidReceiveLocalNotification
中我不想再问他们,我只想带他们去参加活动。
但是,如果当通知触发并调用 application:DidReceiveLocalNotification
时应用程序位于前台,我想询问用户是否要查看该事件。
本质上我会:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (appLauncedFromBackground)
// go to event
else
// ask if user wants to view event
}
我想过在 applicationWillEnterForeground:application
内向 NSUserDefault 写入一个 bool 值,然后在 application:DidReceiveLocalNotification
内读取并重置它。但我想,如果有一种方法可以进行测试,那将是一条更好的路线。
谢谢,马克
编辑************ *************** ***
谢谢 xuzhe,但在这种情况下这是行不通的。这里有一些更详细的信息,可以解释为什么它不起作用,也许可以帮助别人为我回答这个问题:
我在此处设置的 UILocalNotification
是一个事件,例如计划触发的日历事件用户选择的时间,因此在安排通知时我将不知道通知触发时用户将做什么。我正在使用通知的 userInfo 来存储有关已安排事件的数据,但按照建议设置像 inBackground 这样的值将不起作用。
我确实知道如果用户决定要“查看”事件,这将是两种情况之一。 (这一切都假设应用程序位于后台或前台并且不退出)。
1 - 如果在应用程序未使用时触发&&该应用程序在后台运行并且不会退出,iOS 将通知用户发生了事件,并提供一个选项以转到负责的应用程序(在本例中是我的应用程序)。此时,假设用户说“是的,带我去这个很酷的应用程序”,它将从后台打开应用程序并调用应用程序:DidReceiveLocalNotification,我可以在其中获取所有通知用户信息。
2 - 当计划的事件发生时,用户碰巧在我的应用程序中。此时,再次调用 application:DidReceiveLocalNotification 方法,然后我可以在其中获取所有通知 userInfo。
但此时我想知道这两个场景中哪一个刚刚发生了 1 或 2。
希望有关我使用 UILocalNotification
的附加详细信息将有助于回答这个问题。提前致谢 - 标记
结束编辑************ *************** *
When receiving a UILocalNotification
- The method
application:DidReceiveLocalNotification
is called when the app is in Foreground. - When the app is in the Background, it opens the application and then calls
application:DidReceiveLocalNotification
.
How can I determine which one of these scenarios are taking place? I would like to treat them differently.
If the app was in the Background, then the user indicated they want to look at the event. So in application:DidReceiveLocalNotification
I don't want to ask them again, I just want to take them to the event.
But if the application is in the Foreground when the notification fires and calls application:DidReceiveLocalNotification
, I want to ask the user if they want to view the event.
In essence I would have:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (appLauncedFromBackground)
// go to event
else
// ask if user wants to view event
}
I've thought of writing a bool value to an NSUserDefault inside applicationWillEnterForeground:application
and then reading and resetting it inside application:DidReceiveLocalNotification
. But I thought if there was a way to test, that would be a much better route.
Thanks, Mark
EDIT ******************************
Thanks xuzhe but that isn't going to work in this situation. Here is a little more detail that may explain why it wont work and maybe help someone in answering this for me:
The UILocalNotification
that I am setting here is an event like a calendar event that is scheduled to fire at a user selected time so when scheduling the notification I will have no idea what the user is going to be doing when the notification fires. I am using the userInfo of the notification to store data about the event that is scheduled but setting a value like inBackground as suggested wont work.
I do know it will be one of two situations if the user decides they want to "view" the event. (this all will assume the app is either in the background or foreground and NOT quit).
1 - If it fires while the app is not being used && the app is in the background and not quit, iOS will notify the user that an event has occurred and provide an option for going to the responsible app (which is mine in this case). At that point assuming the user says "yes take me to this cool app" it will open the app from the background and call the application:DidReceiveLocalNotification where I can get all the notification userInfo.
2 - The user, by chance, happens to be in my application when the scheduled event takes place. At that point the application:DidReceiveLocalNotification method is called again where I can then get all the notification userInfo.
But it is at this point that I want to know which of the two scenarios just took place 1 or 2.
Hope this additional detail about my use of the UILocalNotification
will help in answering this question. Thanks in advance - mark
END EDIT ****************************
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许最简单的方法是当您的应用程序位于前台时,不要发送 LocalNotification,而只是将您的用户带到活动中。
但如果您坚持使用 LocalNotification 来做到这一点,这里有一个简单的方法来检测 UILocalNotification 触发时您的应用程序的状态。
Maybe the easiest way is when your App is in Foreground, do not send a LocalNotification but just take your users to the event.
But if you insist on doing it by using LocalNotification, here is an easy way to detect what's your App's status when UILocalNotification fired.
这是我添加到这些函数体内的内容......
application didFinishLaunchingWithOptions
:在
applicationDidEnterBackground
中:在
applicationWillEnterForeground
中:applicationDidBecomeActive
:didReceiveLocalNotification
:现在两个通知消失了(特别是当应用程序处于后台并且您从通知警报中打开它时出现的通知)。
This is what I added to the bodies of these functions...
application didFinishLaunchingWithOptions
:In
applicationDidEnterBackground
:In
applicationWillEnterForeground
:applicationDidBecomeActive
:didReceiveLocalNotification
:Now the two notifications are gone (particularly the one appearing when the app is in background and you open it from the notification alert.
在
didReceiveLocalNotification
中,您可以检查应用程序状态:[UIApplication shareApplication].applicationState
如果它等于
UIApplicationStateInactive
,您就知道该应用程序处于后台用户通过本地通知警报打开它。如果是
UIApplicationStateActive
您可能想向用户显示您自己的警报。In
didReceiveLocalNotification
you can check the application state:[UIApplication shareApplication].applicationState
If it's equal to
UIApplicationStateInactive
you know that the app was in the background and the user opened it with the local notification alert.if it's
UIApplicationStateActive
you may want to display your own alert to the user.