【问题标题】:How to Remove Repeating Local Notifications Wisely (Swift 4)?如何明智地删除重复的本地通知(Swift 4)?
【发布时间】:2018-03-27 21:35:11
【问题描述】:

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

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

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

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

结束

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

【问题讨论】:

    标签: ios swift notifications local lifecycle


    【解决方案1】:

    当应用程序即将变为非活动状态时,您几乎没有选择:

    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 生命周期方法中添加和删除通知是一种很好的做法,而不是在应用程序级生命周期中

    【讨论】:

    • 感谢您的回复。我的回复恰好太长,所以我把它作为答案放在下面。请看一下。
    • 我看不到,也不应该被允许,你可以在这里简单解释一下,而不是很长的回复
    • 感谢您的回复。当应用程序即将或已经进入后台时,通知应该保留,因为这正是需要这些通知的状态(否则为什么还要麻烦实现这些通知?)当应用程序已经暂停时(在 bg 中),它无法观察应用程序终止(appWillTerminate),这就是为什么如果具有活动通知的应用程序已经在 bg 中,然后被用户强制退出(应用程序终止,通知待处理),它“由于信号 9 而终止”。这可能是因为应用没有机会响应此用户行为。
    • 我们谈论的是本地通知(不是远程通知)。所以,操纵视图控制器的生命周期并不能解决这个问题。我使用 view did load 和 deinit 方法在加载和关闭 vc 时添加和删除通知,但是当应用程序强制退出时,不会调用 deinit 方法。我还通过观察 AppDelegate 的方法添加/删除通知,它们工作得很好,除了 appWillTerminate,它仅在应用程序未暂停时调用。唯一的出路是想出一个解决方法,在后台调用 appWillTerminate。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2020-09-19
    • 2011-09-14
    相关资源
    最近更新 更多