我需要清除通知中心中的所有通知。我尝试cancellAllLocalNotification
和UIApplication.shared.applicationIconBadgeNumber = 0
无效,但是我使用的是swift 4,xcode 10.1和ios12。任何人都可以帮助我。
答案 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.
使用此方法从“通知中心”删除您所有应用传递的通知。该方法异步执行,立即返回并删除后台线程上的标识符。此方法不会影响已安排但尚未传递的任何通知请求。
此方法异步执行,删除辅助线程上的所有未决通知请求。
答案 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
}
}