[英]Remove “local or remote notifications” from notification center
我需要从通知中心清除所有通知。我试图cancellAllLocalNotification
和UIApplication.shared.applicationIconBadgeNumber = 0
但无法正常工作。我使用的是swift 4,xcode 10.1和ios 12.任何人都可以帮助我。
cancellAllLocalNotification
is deprecated from iOS 10. from appledoc cancellAllLocalNotification
在IOS 10.弃用appledoc
Deprecated 不推荐使用
Use the UNUserNotificationCenter class to schedule local notifications instead. 改用UNUserNotificationCenter类安排本地通知。
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 从交付的删除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 从待删除的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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.