我希望用户在退出应用程序时不会收到通知
我尝试的是检测用户何时退出应用程序,以及用户是否退出执行以下代码的应用程序:UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
所以我尝试通过这个函数:optional func applicationWillTerminate(_ application: UIApplication)来检测是否使用了退出应用程序,但是我不确定当用户退出应用程序并同时执行以下代码时应该在哪里以及如何调用它:UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
有谁能帮帮我吗?
发布于 2020-02-28 20:11:23
你可以尝试下面这样的方法
func applicationWillTerminate(_ application: UIApplication) {
application.unregisterForRemoteNotifications()
}如果您希望应用程序在进入后台后立即停止所有通知,您可以使用
func applicationDidEnterBackground(_ application: UIApplication) {
application.unregisterForRemoteNotifications()
}注意:如果您使用第二个选项,请不要忘记在应用程序再次进入前台时重新注册推送通知
发布于 2020-02-28 20:09:26
此代码示例删除所有挂起的通知。
func applicationWillTerminate(_ application: UIApplication) {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}此代码示例用于注销远程通知。
func applicationWillTerminate(_ application: UIApplication) {
UIApplication.shared.unregisterForRemoteNotifications()
}https://stackoverflow.com/questions/60451321
复制相似问题