cancelAllLocalNotifications not working in iOS10

前端 未结 3 1781
逝去的感伤
逝去的感伤 2021-01-01 22:09

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 22:16

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

提交回复
热议问题