我一直在做我的一个项目,我允许用户在他们想要的时间安排多个通知。
iOS 10 让我们能够使用 DateComponents 作为通知的触发日期,但我对如何安排和处理多个通知有点迷茫。
每个通知都需要自己的请求,然后又需要自己的标识符,否则您无法创建多个通知。
我想我必须使用标识符来安排和处理通知,但现在我的代码是如此混乱,以至于我一直在认真地问自己是否没有更简单的方法来做到这一点。
根据我的数据模型,通知唯一标识符由以下部分组成:
- 我的模型的 id
- 每次创建新通知时递增的数字
- 上面用下划线分隔
因此,例如,如果我必须为 id 为 3 的对象安排 10 个通知,它将如下所示:3_1, 3_2, 3_3...
每次我收到通知时,我都会循环通过收到的通知来更新我的 UI。当用户希望删除接收到的特定模型的通知时,我会通过检查以相同 ID 开头的标识符来循环接收通知的唯一标识符。
我真的不知道如何才能做到这一点,因为根据文档,删除传递的通知的唯一方法是使用标识符:UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers:)
问题是,它产生了各种各样的问题,虽然我可以很容易地纠正它们,但它看起来很老套。我并不为我所做的事情感到骄傲,我正在寻找更聪明的方法来解决它。我故意没有发布任何代码,因为那不是问题。方法是真正的问题。
我在这里问是因为该UserNotifications
框架非常新,而且我无法找到有关该主题的资源。
任何想法 ?提前致谢。
编辑:这是一些代码。
@available(iOS 10.0, *)
func checkDeliveredAndPendingNotifications(completionHandler: @escaping (_ identifierDictionary: Dictionary<String, Int>) -> ()) {
var identifierDictionary:[String: Int] = [:]
UNUserNotificationCenter.current().getDeliveredNotifications { (notifications) in
for notification in notifications {
let identifierArraySplit = notification.request.identifier.components(separatedBy: "_")
if identifierDictionary[identifierArraySplit[0]] == nil || identifierDictionary[identifierArraySplit[0]]! < Int(identifierArraySplit[1])! {
identifierDictionary[identifierArraySplit[0]] = Int(identifierArraySplit[1])
}
}
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (requests) in
for request in requests {
let identifierArraySplit = request.identifier.components(separatedBy: "_")
if identifierDictionary[identifierArraySplit[0]] == nil || Int(identifierArraySplit[1])! > identifierDictionary[identifierArraySplit[0]]! {
identifierDictionary[identifierArraySplit[0]] = Int(identifierArraySplit[1])
}
}
completionHandler(identifierDictionary)
})
}
}
@available(iOS 10.0, *)
func generateNotifications() {
for medecine in medecines {
self.checkDeliveredAndPendingNotifications(completionHandler: { (identifierDictionary) in
DispatchQueue.main.async {
self.createNotification(medecineName: medecine.name, medecineId: medecine.id, identifierDictionary: identifierDictionary)
}
})
}
}
@available(iOS 10.0, *)
func createNotification(medecineName: String, medecineId: Int identifierDictionary: Dictionary<String, Int>) {
let takeMedecineAction = UNNotificationAction(identifier: "TAKE", title: "Take your medecine", options: [.destructive])
let category = UNNotificationCategory(identifier: "message", actions: [takeMedecineAction], intentIdentifiers:[], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
let takeMedecineContent = UNMutableNotificationContent()
takeMedecineContent.userInfo = ["id": medecineId]
takeMedecineContent.categoryIdentifier = "message"
takeMedecineContent.title = medecineName
takeMedecineContent.body = "It's time for your medecine"
takeMedecineContent.badge = 1
takeMedecineContent.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
var takeMedecineIdentifier = ""
for identifier in identifierDictionary {
if Int(identifier.key) == medecineId {
let nextIdentifierValue = identifier.value + 1
takeMedecineIdentifier = String(medecineId) + "_" + String(nextIdentifierValue)
}
}
let takeMedecineRequest = UNNotificationRequest(identifier: takeMedecineIdentifier, content: takeMedecineContent, trigger: trigger)
UNUserNotificationCenter.current().add(takeMedecineRequest, withCompletionHandler: { (error) in
if let _ = error {
print("There was an error : \(error)")
}
})
}
在该checkDeliveredAndPendingNotifications
方法中,我遍历所有未决和已传递的通知,因此稍后我可以创建一个尚不存在的标识符。我还没有找到另一种为每个通知生成唯一标识符的方法。
由于大部分工作是在另一个异步队列上完成的,所以当他完成工作时,我还创建了一个完成处理程序。然后我在主线程上调用该createNotification
方法(因为我使用的是 Realm,而且我也有义务这样做),它应该创建一个通知。
这里的问题是func add(UNNotificationRequest, withCompletionHandler: (Error?) -> Void)? = nil)
异步工作的方法。所以当我回到我的循环中generateNotifications
返回checkDeliveredAndPendingNotifications
不正确的数据时。好吧,没有错,只是通知尚未创建...
我是一个线程的菜鸟,我被这些操作困住了,我不知道该去哪里。我不确定我是否以正确的方式解决问题。