我正在使用UNNotificationServiceExtension替换通知中的一个电话号码。我试图在CNContactStore中查找电话号码,并将Ph#替换为联系人名称。
我的问题是,当我调用CNContactStore enumerateContacts(with: keysToFetch:)时,这个扩展名就会退出,而不会从CNContactStore调用返回。
另一方面,如果我调用CNContactStore的unifiedContacts(匹配:谓词,keysToFetch:键),它将按预期返回。但不幸的是,这个电话找不到电话号码。我唯一能查到电话号码的方法就是打电话给enumerateContacts.
我使用相同的代码在我的应用程序中查找电话号码,它运行良好。我还可以替换通知扩展w/o问题中的文本。只有当我试图在扩展中调用enumerateContacts时,问题才会发生。
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
let searchPhoneNumber = "5555551234"
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] as [CNKeyDescriptor]
let contactsStore = CNContactStore()
do {
try contactsStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keys)) {
(contact, error) -> Void in
print("We never get here!!!")
if (!contact.phoneNumbers.isEmpty) {
for phoneNumber in contact.phoneNumbers {
if phoneNumber.value.stringValue == searchPhoneNumber {
// swap number for name
self.bestAttemptContent?.body = contact.givenName
contentHandler(self.bestAttemptContent!)
return
}
}
}
}
}
catch {
print("And we never get here.")
contentHandler(bestAttemptContent!)
return
}
contentHandler(bestAttemptContent!)
}发布于 2018-10-15 11:54:06
来自UNNotificationServiceExtension https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
该方法执行其任务和执行所提供的完成块的时间有限。如果您的方法没有及时完成,系统将调用serviceExtensionTimeWillExpire()方法,给您最后一次提交更改的机会。如果在过期之前不更新通知内容,系统将显示原始内容。
我猜循环遍历联系人超过了分配给方法完成的时间。
https://stackoverflow.com/questions/49742192
复制相似问题