UILocalNotification - 当应用程序未运行时如何处理?
我现在已经在两个不同的应用程序中实现了 UILocalNotifications。一个使用位置服务,这意味着如果它被杀死,它(通常)会重新启动(所以这不是一个大问题)。但是,在另一个应用程序中,我根据时间安排 UILocalNotification
。在这种情况下,我遇到了一个大问题。
如果我安排通知,那么应用程序会被终止,内存不足,手机关闭然后再次打开,打开应用程序时我无法“自动”查看较旧的通知。
这是有效的工作流程(应用程序在后台运行):
- 启动应用程序;应用程序安排通知;关闭应用程序;应用程序现在在后台运行
- 接收本地通知;一开始忽略它;从顶部(状态栏)拉出下拉菜单;触摸通知以启动应用程序
- 结果:应用程序正确显示通知中的信息。
以下是不起作用的工作流程(应用程序不再在后台运行):
- 启动应用程序;应用程序安排通知;关闭应用程序; app现在正在后台运行
- 手动杀死app(模拟我的情况)
- 仍然收到本地通知;一开始忽略它;从顶部(状态栏)拉出下拉菜单;触摸通知以启动应用程序
- 结果:应用程序启动,但未调用
didReceiveLocalNotification
方法。用户认为该应用程序无法运行。
注意:我什至无法手动强制显示这些信息,因为如果他们收到了多个通知,我无法判断他们触摸了哪一个来知道要显示哪一个。
当应用未在后台运行(因此不运行didReceiveLocalNotification方法
)时,有什么方法可以知道他们触及了哪个通知?
I have implemented UILocalNotifications
in two different apps now. One uses Location Services, which means if it gets killed, it (usually) gets restarted (so it's not as big of an issue). However, in another app, I schedule a UILocalNotification
based upon time. In this case, I'm having a major problem.
If I schedule a notification, then the app gets killed, pushed out of memory, phone is turned off then on again, I cannot view older notifications "automatically" when opening the app.
Here is the workflow that works (app running in the background):
- Launch app; app schedules notification; close app; app is now running in the background
- Receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
- Results: The app appropriately displays the information in the notification.
Here is the workflow that does not work (app no longer running in the background):
- Launch app; app schedules notification; close app; app is now running in the background
- Manually kill app (to simulate my situation)
- Still receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
- Results: The app launches, but the
didReceiveLocalNotification
method is not called. The user thinks the app doesn't work.
Note: I can't even manually force the information, because if they have received more than one notification, I can't tell which one they touched on to know which one to display.
Is there any way to know which notification they touched on when the app is not running in the background (and thus does not run the didReceiveLocalNotification method
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第二种情况下,用户已退出(或终止)应用程序,因此当用户触摸通知时,您的应用程序将启动。
这可以在下面提到的 appDelegate 的方法
didFinishLaunchingWithOptions
中处理,该方法在应用程序启动时调用,因此只需检查应用程序是否因用户触摸通知而实际启动(代码如下):In the 2nd case, the user has quit (or killed) the app, so when the user touches the notification your app would be launched.
This can be handled in the below mentioned appDelegate's method
didFinishLaunchingWithOptions
is invoked when an app is launched, so just check if the app was actually launched because the user touched the notification (code below):@Ranjit,当用户点击应用程序图标时,这将起作用,因为这是一个应用程序委托方法。每次应用程序启动完成时都会接到电话。 (无法发表评论,因为我缺乏声誉......;))
@Ranjit, this will work when user tap the app icon because is an app delegate method. Get called every time the app finished launching. (Can´t comment because my lack of reputation... ;) )