简体   繁体   English

cancelAllLocalNotifications在iOS10中不起作用

[英]cancelAllLocalNotifications not working in iOS10

I want to remove all previous local notification from NotificationCenter when adding new notifications. 我想在添加新通知时从NotificationCenter中删除所有以前的本地通知。 But it is working in iOS9.0 and lower version but in iOS 10 it fires multiple local notifications. 但它适用于iOS9.0及更低版本,但在iOS 10中它会触发多个本地通知。 So it seems like cancelAllLocalNotifications not clearing notifications. 所以似乎cancelAllLocalNotifications没有清除通知。

Code compile successfully in iOS10. 代码在iOS10中成功编译。

UIApplication.shared.cancelAllLocalNotifications()

For iOS 10, Swift 3.0 对于iOS 10,Swift 3.0

cancelAllLocalNotifications deprecated from iOS 10. 从iOS 10弃用了cancelAllLocalNotifications

 @available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]") open func cancelAllLocalNotifications() 

You will have to add this import statement, 你必须添加这个import语句,

import UserNotifications

Get notification center. 获取通知中心。 And perform the operation like below 并执行如下操作

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.

If you want to remove single or multiple specific notification, you can achieve it by below method. 如果要删除单个或多个特定通知,可以通过以下方法实现。

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

Hope it helps..!! 希望能帮助到你..!!

For iOS 10, Objective C : 对于iOS 10,目标C

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
[center removeAllPendingNotificationRequests];

Swift 4 : 斯威夫特4

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

If you want to list all notifications: 如果要列出所有通知:

func listPendingNotifications() {

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

For iOS 10, You can use this way for removing all of Local Notification. 对于iOS 10,您可以使用这种方式删除所有本地通知。 I have just tested, It's working. 我刚刚测试过,它正在工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM