【问题标题】:Wait till local notifications from UNUserNotificationCenter gets deleted using removePendingNotificationRequests ios 10 swift 3等到来自 UNUserNotificationCenter 的本地通知被删除使用 removePendingNotificationRequests ios 10 swift 3
【发布时间】:2017-11-03 19:33:51
【问题描述】:

使用来自UNUserNotificationCenter 的新本地通知。 我尝试删除带有一些标识符的通知:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)

并来自文档:

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

完成处理程序不存在。那么我怎么知道它什么时候真正被删除呢?在继续之前,我需要确保该标识符不再存在。

我知道我可以使用下一个代码

notificationCenter.getPendingNotificationRequests { (requests) in
        for request in requests {
         }
}

但如果我在删除后立即运行此代码 - 它们仍然存在。但是经过一段时间和后来的代码,它们就消失了。尤其是当您即将达到 64 条通知的限制时,在添加新通知之前这一点很重要

【问题讨论】:

    标签: ios swift3 ios10 unusernotificationcenter


    【解决方案1】:

    您不必执行您在答案中发布的所有复杂逻辑。您可以简单地调用removeAllPendingNotificationRequests 并等待它在getPendingNotificationRequests 方法中完成。您可以在下面运行此代码,看看会发生什么。您将看到after remove 将立即打印,然后是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)
        }
    

    之所以如此,是因为它们都是放入队列中并一一运行的任务。因此,它首先放置 63 条通知,然后删除它们,最后对它们进行计数。所有这些任务严格一个接一个地进行

    【讨论】:

      【解决方案2】:

      好吧,好像过了一会儿我找到了一种方法——它的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中不存在已删除的通知

      【讨论】:

      • 我认为它不起作用,因为 removePendingNotificationRequests 仍将在不同的线程上运行,因此该组将立即离开。如果 group.leave 在 removePendingNotificationRequests 所在的线程中被调用,它会起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 2017-05-20
      • 1970-01-01
      相关资源
      最近更新 更多