[英]How can I dismiss UILocalNotifications that appear when my app is in the background?
I'm working on an iPhone app that needs to remind a user to check in at regular intervals using UILocalNotifications. 我正在开发一款iPhone应用,需要提醒用户使用UILocalNotifications定期办理登机手续。 If they don't check in for a few hours, they may be reminded a couple times, but I just want to show the latest notification.
如果他们没有办理登机手续几个小时,可能会提醒他们几次,但我只是想显示最新的通知。
Now, if the app is open, I get a callback to didReceiveLocalNotification:(UILocalNotification *)notification, and I can keep track of whether there's a notification showing. 现在,如果应用程序处于打开状态,我会收到didReceiveLocalNotification :( UILocalNotification *)通知的回调,我可以跟踪是否有通知显示。
If the app is not running and the user clicks the -action- button, I get a callback to 如果应用程序未运行且用户单击-action-按钮,则会收到回调
so I can dismiss any old notifications. 所以我可以解雇任何旧的通知。 However, if they click 'cancel', they have to click 'cancel' on a bunch of layered notifications, because as far as I can tell I don't get a callback (how could I, if the app isn't launched) and it doesn't seem like there's a flag or something when creating the UILocalNotification to have newer reminders from an app automatically dismiss other ones.
但是,如果他们单击“取消”,他们必须在一堆分层通知上单击“取消”,因为据我所知,我没有得到回调(如果应用程序未启动,我该怎么办)并且在创建UILocalNotification时,似乎没有标记或其他内容,以便从应用程序中自动关闭其他提醒。
If the app is in the background but running, it's worse - first, I don't get any sort of callback there if the user click's cancel, so I have the same problem - the user has to click cancel a bunch of times. 如果应用程序在后台但正在运行,那就更糟了 - 首先,如果用户单击取消,我没有得到任何类型的回调,所以我遇到了同样的问题 - 用户必须单击取消很多次。 Second, if they click the action, I get a call to ApplicationDidBecomeActive, but there's no distinguishing between that and when the user just switches back and forth;
其次,如果他们单击动作,我会调用ApplicationDidBecomeActive,但是没有区别,当用户只是来回切换时; I can dismiss and reschedule them here, but it doesn't seem to work perfectly, sometimes a few pop up before they're dismissed.
我可以在这里解雇并重新安排它们,但它似乎并不完美,有时会在它们被解雇之前弹出一些。
Any suggestions? 有什么建议么? If there were a way for the notifications to expire automatically that would be great too.
如果有一种方法可以让通知自动过期,那也很棒。 I've looked online a bit and haven't found much help, but it seems like a big oversight, so hopefully there's some way to handle this gracefully.
我在网上看了一下,并没有找到太多的帮助,但这似乎是一个很大的疏忽,所以希望有一些方法来优雅地处理这个。
Thanks. 谢谢。
You won't be able to get any callback when the user "cancel"s as you pointed out. 当您指出用户“取消”时,您将无法获得任何回调。
Is it possible to just remind the user once in your case? 在您的情况下,是否可以只提醒用户一次? Only schedule one notification at a time and renew it on app launch/resume.
仅一次安排一个通知,并在应用启动/恢复时续订。
I have not tried the following yet, but I believe this can be a work around for your case. 我还没有尝试过以下内容,但我相信这可以解决您的问题。 Use
CLLocationManager
's startMonitoringSignificantLocationChanges
使用
CLLocationManager
的startMonitoringSignificantLocationChanges
Every time the device's location changes significantly, your app will be launched in the background with options passed to locationManager:didUpdateLocations:
and you can probably schedule a UILocalNotification
from there! 每当设备的位置发生显着变化时,您的应用程序将在后台启动,并将选项传递给
locationManager:didUpdateLocations:
您可以从那里安排UILocalNotification
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.