从通知中心删除“本地或远程通知”

时间:2019-01-30 11:08:25

标签: ios swift notifications

我需要清除通知中心中的所有通知。我尝试cancellAllLocalNotificationUIApplication.shared.applicationIconBadgeNumber = 0无效,但是我使用的是swift 4,xcode 10.1和ios12。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

cancellAllLocalNotification已从iOS 10中弃用。appledoc

  

已弃用

     

使用UNUserNotificationCenter类来安排本地通知。

使用

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

来自Delivered Removal appledoc

  

使用此方法从“通知中心”删除您所有应用传递的通知。该方法异步执行,立即返回并删除后台线程上的标识符。此方法不会影响已安排但尚未传递的任何通知请求。

来自Pending Removal appledoc

  

此方法异步执行,删除辅助线程上的所有未决通知请求。

答案 1 :(得分:0)

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