等待使用removePendingNotificationRequests ios 10 swift 3删除来自UnuseNotificationCenter的本地通知
使用来自等待使用removePendingNotificationRequests ios 10 swift 3删除来自UnuseNotificationCenter的本地通知,ios,swift3,ios10,unusernotificationcenter,Ios,Swift3,Ios10,Unusernotificationcenter,使用来自UNUserNotificationCenter的新本地通知。 我尝试删除带有一些标识符的通知: UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers) 从文件中: 此方法异步执行,删除辅助线程上挂起的通知请求 完成处理程序不存在。那我怎么知道它什么时候真的被删除了呢?在继续之前,我需要确保这个标识符不再存在 我知道我可以使用下一个代码 noti
UNUserNotificationCenter
的新本地通知。
我尝试删除带有一些标识符的通知:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
从文件中:
此方法异步执行,删除辅助线程上挂起的通知请求
完成处理程序不存在。那我怎么知道它什么时候真的被删除了呢?在继续之前,我需要确保这个标识符不再存在
我知道我可以使用下一个代码
notificationCenter.getPendingNotificationRequests { (requests) in
for request in requests {
}
}
但是,如果我在删除后立即运行此代码,它们仍然存在。但是过了一段时间之后,在代码中它们就消失了。特别重要的是,在添加新通知之前,当您即将丰富64个通知的限制时,这一点非常重要。过了一段时间,我找到了一种方法—its
DispatchGroup
它也以异步方式与userInteractive
质量:
let dq = DispatchQueue.global(qos: .userInteractive)
dq.async {
let group = DispatchGroup()
group.enter()
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arr)
group.leave()
group.notify(queue: DispatchQueue.main) { () in
UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
for request in requests {
print("||=> Existing identifier is \(request.identifier)")
}
}
}
}
而且,已删除的通知不存在于
getPendingNotificationRequests
中,您不必像在答案中发布的那样执行所有复杂的逻辑。您只需调用removeAllPendingNotificationRequests
并等待在getPendingNotificationRequests
方法中完成。您可以运行下面的代码,看看会发生什么。您将看到删除后将立即打印,然后是1..63
中的数字,然后是0
let notificationCenter = UNUserNotificationCenter.current()
for i in 1 ... 63 {
let components: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
let date = Calendar.current.date(byAdding: .hour, value: 1, to: Date())!
let dateComponents = Calendar.current.dateComponents(components, from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "id" + String(i),
content: content, trigger: trigger)
notificationCenter.add(request) {
(error) in
print(i)
}
}
notificationCenter.removeAllPendingNotificationRequests()
print("after remove")
notificationCenter.getPendingNotificationRequests() {
requests in
print(requests.count)
}
let notificationCenter=unuservificationcenter.current()
因为我在1。。。63 {
let组件:Set=[.year、.month、.day、.hour、.minute、.second]
让date=Calendar.current.date(通过添加:.hour,值:1,to:date())!
让dateComponents=Calendar.current.dateComponents(组件,从:日期)
let trigger=UNCalendarNotificationTrigger(日期匹配:dateComponents,重复:false)
let content=UNMutableNotificationContent()
content.title=“title”
content.body=“body”
content.sound=UNNotificationSound.default()
let request=UNNotificationRequest(标识符:“id”+字符串(i),
内容:内容,触发器:触发器)
通知中心。添加(请求){
(错误)在
印刷品(一)
}
}
notificationCenter.removeAllPendingNotificationRequests()
打印(“删除后”)
notificationCenter.getPendingNotificationRequests(){
请求
打印(请求.计数)
}
之所以如此,是因为它们都是放入队列并逐个运行的任务。因此,它首先放置63个通知,然后删除它们,最后统计它们。所有这些任务严格地一个接一个地执行我认为这不起作用,因为removePendingNotificationRequests仍将在不同的线程上运行,因此组将立即离开。如果在removePendingNotificationRequests所在的线程中调用group.leave,它将起作用