简体   繁体   中英

Remove “local or remote notifications” from notification center

我需要从通知中心清除所有通知。我试图cancellAllLocalNotificationUIApplication.shared.applicationIconBadgeNumber = 0但无法正常工作。我使用的是swift 4,xcode 10.1和ios 12.任何人都可以帮助我。

cancellAllLocalNotification is deprecated from iOS 10. from appledoc

Deprecated

Use the UNUserNotificationCenter class to schedule local notifications instead.

Use

UNUserNotificationCenter.current().removeAllDeliveredNotifications() // For removing all delivered notification
UNUserNotificationCenter.current().removeAllPendingNotificationRequests() // For removing all pending notifications which are not delivered yet but scheduled. 

From Delivered Removal appledoc

Use this method to remove all of your app's delivered notifications from Notification Center. The method executes asynchronously, returning immediately and removing the identifiers on a background thread. This method does not affect any notification requests that are scheduled, but have not yet been delivered.

From Pending Removal appledoc

This method executes asynchronously, removing all pending notification requests on a secondary thread.

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
        print("payload",payload.dictionaryPayload)
{
 let state = UIApplication.shared.applicationState
        if state == .background  || state == .inactive{
         startTimer()
}
}

func startTimer() {

        timer2 = Timer.scheduledTimer(timeInterval: 7, target: self, selector: (#selector(updateTimer2)), userInfo: nil, repeats: true)
 }
@objc func updateTimer2() {

        seconds2 += 7
        isPushNotificationCallReceived = true
        let content = UNMutableNotificationContent()
        content.title = self.message
        content.body = self.callerName
        content.badge = 0
        content.sound = UNNotificationSound(named: "phone_loud.wav")
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
        let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)]
        print(seconds2)

        if seconds2 == 28 {

            isPushNotificationCallReceived = false
            seconds2 = 0
            timer2.invalidate()

 UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers:
 ["SimplifiedIOSNotification"])
 UIApplication.shared.applicationIconBadgeNumber = 0
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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