0

我一直在做我的一个项目,我允许用户在他们想要的时间安排多个通知。我正在使用UserNotificationsiOS 10 中的新功能。

为了正确安排所有通知,每个通知都需要有自己的唯一标识符。我根据我的数据模型组成了我的:

  1. 我的模型的 id
  2. 每次创建新通知时递增的数字
  3. 上面用下划线分隔

因此,例如,如果我必须为 id 为 3 的对象安排 15 个通知,它将如下所示:3_1, 3_2, 3_3...3_15

这是我的做法:

@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确保稍后我将创建尚不存在的标识符。

当它完成工作后,我调用createNotificationwhich 使用前一个函数返回的字典来生成正确的标识符。

例如,如果有 5 个通知在磁盘上传递,另外 10 个在等待 id 为 3 的模型,它看起来像这样:

["3" : 15]

createNotification只是简单地获取字典中的值并将其增加 1 以创建标识符。

真正的问题是:

UNUserNotificationCenter.current().add(takeMedecineRequest, withCompletionHandler: { (error) in
            if let _ = error {
                print("There was an error : \(error)")
            }
        })

这是一个异步任务。考虑到它不会等待,一旦我回到我的循环中,generateNotifications不会checkDeliveredAndPendingNotifications返回正确的字典,因为通知没有完成创建。

考虑到上面的例子,如果我必须安排 3 个通知,我想得到这样的东西:

print("identifierDictionary -> \(identifierDictionary)") // ["3":15], ["3":16], ["3":17]
print("unique identifier created -> \(takeMedecineIdentifier") // 3_16, 3_17, 3_18

但现在我得到:

print("identifierDictionary -> \(identifierDictionary)") // ["3":15], ["3":15], ["3":15]
print("unique identifier created -> \(takeMedecineIdentifier") // 3_16, 3_16, 3_16

那么,我如何才能等待这个异步调用完成,然后再返回我的循环呢?

在此先感谢您的帮助。

4

1 回答 1

1

如果您不需要能够从标识符中“读取”它是哪个通知,则可以使用随机字符串作为标识符。

即使可以像现在这样正确生成唯一的 id,也不应该依赖控制流来生成正确的 id。这通常被认为是不好的编码实践,尤其是在依赖(第 3 方)库或 API 时。一项更改可能会破坏它。

您可以按照此处所述生成随机字符串。使用 24 个字符的字母数字字符串给出 (36+36+10)^24 组合,使碰撞的机会可以忽略不计。

您可以使用 userinfo 字典或其他一些持久性方法将标识符与特定通知相关联。如果您正在使用CoreData,您可以将具有唯一标识符的通知对象与医学请求相关联。

于 2016-10-13T15:00:58.743 回答