ios - 如何明智地删除重复的本地通知(Swift 4)?

标签 ios swift notifications local lifecycle

我有一个应用程序,从点击某个按钮(无论是在前台还是后台)的那一刻起,每 30 分钟触发一次本地通知。我学习了如何在点击另一个按钮或关闭 View Controller 时删除这些通知。这是通过以下方式完成的: UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 它工作得很好。但是,如果用户通过在多任务模式下向上滑动来强制退出(手动关闭)应用程序,那么奇怪的事情就会开始发生。默认情况下,终止的应用程序不会删除那些待处理的推送通知,即使应用程序关闭,它们也会继续出现。

我了解到,应用程序生命周期中有几种状态可以终止应用程序。因此,我们可以使用应用程序委托(delegate)中的多种方法来触发上述代码以删除通知(否则它们会不断出现)。

因此,当我们在前台并点击主页按钮(或用 iPhone X 向上滑动)切换到多任务模式(在事件应用程序之间进行选择或删除它们)时,这是 applicationWillResignActive 的时间 - 我们可以开始观察应用程序终止在这里(也许通过观察 applicationWillTerminate 并使用某种方法作为选择器)并在终止时删除这些通知(奇怪的是,applicationWillTerminate 并不总是自行工作)。

但是,如果您切换到另一个应用程序,然后返回多任务模式,则该应用程序这次处于后台模式(而不是非事件模式)。因此,当应用程序已经处于后台模式时,似乎无法触发某些方法并观察某些事情。请问有吗?在这种情况下,applicationDidEnterBackground 也不起作用。一切都以“来自调试器的消息:由于信号 9 而终止”

结束

那么,当您的应用程序处于后台模式并且即将被用户终止时,如何删除所有待处理的通知?如果它处于后台模式并且不会被用户终止,则通知需要保持事件状态。 如果有解决办法,请帮忙。或者,当 View Controller 被关闭(它不是应用程序中唯一的主视图 Controller )以及当用户在非事件或后台状态下终止/关闭应用程序时,也许有更好的方法来删除挂起的(事件的)本地通知?感谢您的帮助。

最佳答案

当应用程序即将变为非事件状态时,您几乎没有什么选择:

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

就您而言,您可能应该删除所有通知中的通知,然后在补充它们的其他功能中再次添加它们,例如:

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

我相信在 UIViewController 生命周期方法中添加和删除通知是一种很好的做法,而不是在应用程序级别生命周期中

关于ios - 如何明智地删除重复的本地通知(Swift 4)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49522778/

相关文章:

javascript - Fullpage.js 在横向模式下无法在 iOS 上正确缩放

ios - 计算从当前位置到 x 个对象的距离,并使用从最近到最远的过滤器对它们进行排序

ios - 如何将 UIDocumentPickerViewcontroller 文件上传到 AWS S3 TransferUtility Swift

swift - 含 UIColor 扩展名的 'methodName' 使用不明确

swift - 如何从 NWEndpoint.service 枚举案例解析地址和端口信息(如果可能)

http - AWS SNS HTTP/HTTPS 通知的源 IP 是什么?

ios - 删除 UIActivityViewController 中的换行符

iphone - 我可以在 UITextView 中添加换行符和 HTML 链接吗?

ios - 将通知负载发送到 UIViewController

android - FirebaseMessagingService 仅在后台显示通知