Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cancelAllLocalNotifications在iOS10中不工作_Ios_Swift_Uilocalnotification_Ios10 - Fatal编程技术网

cancelAllLocalNotifications在iOS10中不工作

cancelAllLocalNotifications在iOS10中不工作,ios,swift,uilocalnotification,ios10,Ios,Swift,Uilocalnotification,Ios10,添加新通知时,我想从NotificationCenter中删除所有以前的本地通知。但它在iOS9.0及更低版本中工作,但在IOS10中它会触发多个本地通知。因此,似乎cancelAllLocalNotifications没有清除通知 代码在iOS10中编译成功 UIApplication.shared.cancelAllLocalNotifications() 对于iOS 10,Swift 3.0 cancelAllLocalNotifications已从iOS 10弃用 您必须添加此导入语句

添加新通知时,我想从NotificationCenter中删除所有以前的本地通知。但它在iOS9.0及更低版本中工作,但在IOS10中它会触发多个本地通知。因此,似乎
cancelAllLocalNotifications
没有清除通知

代码在iOS10中编译成功

UIApplication.shared.cancelAllLocalNotifications()
对于iOS 10,Swift 3.0


cancelAllLocalNotifications
已从iOS 10弃用

您必须添加此导入语句

import UserNotifications
获取通知中心。并执行如下操作

let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
如果您想删除单个或多个特定通知,可以通过以下方法实现

center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])

希望对你有帮助

对于iOS 10,目标C

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
Swift 4

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
如果要列出所有通知:

func listPendingNotifications() {

    let notifCenter = UNUserNotificationCenter.current()
    notifCenter.getPendingNotificationRequests(completionHandler: { requests in
        for request in requests {
            print(request)
        }
    })
}

对于iOS 10,您可以使用这种方式删除所有本地通知。 我刚刚测试过,它正在工作

    NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
    for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ;
    }

对于iOS10通知,您使用的是什么?您使用的是“UserNotifications”框架吗?是的,我使用的是
UNUserNotifications
FrameworkInterest,
cancelAllLocalNotifications()
在iOS 10.2中对我有效。
cancelAllLocalNotifications()
在我链接iOS 10的UserNotification框架之前对我有效。有了这个链接,我可以用旧的API发布本地通知,但是“全部取消”停止工作。我实现了相同的功能。谢谢如果您收到以下错误[UnuseNotificationCenter';您的意思是“NSNotificationCenter”?],请在源文件中导入用户通知,如下所示:#import@marcelo dos santos。这将删除为应用程序发送本地通知。同样,如果用户想要打开通知?预定的本地通知将起作用。请给出在objective-c中打开的示例。@kiran,您应该再次安排通知。我有一种方法可以重新安排我的整个事件,因为我的目标是确保我的用户始终获得最新的信息。我不确定,但如果链接到新的
UserNotifications