cancelAllLocalNotifications not working in iOS10

☆樱花仙子☆ 提交于 2019-12-04 17:49:53

问题


I want to remove all previous local notification from NotificationCenter when adding new notifications. But it is working in iOS9.0 and lower version but in iOS 10 it fires multiple local notifications. So it seems like cancelAllLocalNotifications not clearing notifications.

Code compile successfully in iOS10.

UIApplication.shared.cancelAllLocalNotifications()

回答1:


For iOS 10, Swift 3.0

cancelAllLocalNotifications deprecated from iOS 10.

@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 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..!!




回答2:


For iOS 10, Objective C:

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

Swift 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)
        }
    })
}



回答3:


For iOS 10, You can use this way for removing all of Local Notification. I have just tested, It's working.

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


来源:https://stackoverflow.com/questions/40397552/cancelalllocalnotifications-not-working-in-ios10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!