简体   繁体   English

如何解除我的应用在后台时出现的UILocalNotifications?

[英]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-按钮,则会收到回调

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

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 使用CLLocationManagerstartMonitoringSignificantLocationChanges

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.

相关问题 在后台应用中触发计时器并显示UILocalNotifications - Firing a timer in a background app and showing UILocalNotifications 如果当 iOS 应用程序在后台时 NSTimer 不起作用,我怎样才能让我的应用程序定期唤醒? - If NSTimer does not work when an iOS app is in the background, how can I get my app to wake up periodically? 当应用程序进入后台时关闭弹出窗口 - Dismiss popover when app goes in background 我可以将UILocalNotifications数组写入磁盘吗? - Can I write an array of UILocalNotifications to disk? 进入后台模式时,如何在应用中更新位置管理器和计时器? - How can i update location manager and a timer in my app when it goes to background mode? 当我的iPhone应用程序运行时,如何在后台播放连续音乐? - How can I play continuous music in the background when my iPhone app is running? 当我的应用进入后台时,如何停止Google Analytics(分析)跟踪会话? - How can I stop Google analytics tracking session when my app enters into background? 如何像在Facebook应用程序中那样显示和关闭UIPickerView? - How can I display and dismiss a UIPickerView like in the Facebook app? 后台获取和UILocalNotifications - Background Fetch and UILocalNotifications 如何在我的 iOS APP 中制作可变背景图片 - How can I make a variable background image in my iOS APP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM