- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 UNNotificationServiceExtension 将通知中的电话号码替换为姓名。我正在尝试在 CNContactStore 中查找电话号码,并将 Ph# 替换为联系人姓名。
我的问题是,当我调用 CNContactStore enumerateContacts(with:keysToFetch: ) 时,扩展程序将退出,而不会从 enumerateContacts 调用中返回。
另一方面,如果我调用 CNContactStore 的 unifiedContacts(matching: predicate, keysToFetch: keys) ,它会按预期返回。但不幸的是,此调用找不到电话号码。我发现查找电话号码的唯一方法是调用 enumerateContacts。
我使用相同的代码在我的应用程序中查找电话号码,效果很好。我还可以替换通知扩展中的文本,而不会出现问题。仅当我尝试在扩展内调用 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!)
}
最佳答案
来自 UNNotificationServiceExtension https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
That method has a limited amount of time to perform its task and execute the provided completion block. If your method does not finish in time, the system calls the serviceExtensionTimeWillExpire() method to give you one last chance to submit your changes. If you don't update the notification content before time expires, the system displays the original content.
我猜想循环访问联系人超出了为该方法完成分配的时间。
关于swift - UNNotificationServiceExtension 退出调用 CNContactStore enumerateContacts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49742192/
我无法在 UNNotificationServiceExtension 中使用调试器。 这是我在尝试将调试器附加到 UNNotificationServiceExtension 时所做的: 运行应用
当使用 UNNotificationServiceExtension 时,Apple 会给你 30 秒的时间来下载/处理通知,然后才会掉落锤子。 以下载图片为例,在丰富的通知中使用... 假设我花了
我正在使用 UNNotificationServiceExtension 将图像添加到 iOS APNS 通知。它运行良好,除了即使应用程序处于事件状态(在前台)也会显示通知。当应用处于事件状态时,我
我正在创建一个 UNNotificationServiceExtension,但我想在开发它时在没有实际远程通知的情况下对其进行测试。 是否可以在没有远程通知的情况下触发它? 我已尝试使用 UNNot
我正在开发一个使用 UNNotificationServiceExtension 的应用程序。大多数时候(大约 90%),我的扩展程序运行正确,与后端建立连接,然后显示通知。但似乎有时扩展程序根本不运
我有一个用 Swift 编写的 UNNotificationServiceExtension。它所做的一切: 设置通知标题 设置通知正文 加载图片并调用contentHandler() 这是我在做什么
我正在实现新的 iOS10 扩展以使用丰富的通知。我正在尝试在推送通知上对其进行测试,但没有用,我只是收到一个简单的通知,并没有通过扩展。 我做了官方网站和其他一些地方指定的所有内容: 我的应用程序已
我正在尝试在 ios 10 中实现丰富的远程通知。我已经实现了这段代码。收到通知后的控件就到这里了,不知道怎么下载图片显示在通知里。提前致谢。 class NotificationService: U
为了获得丰富的推送通知,我一步步前进。他们在这里: 使用 plist 创建通知服务扩展: NotificationService didRecieve : override func didRecei
我在应用程序中添加了UNNotificationServiceExtension,我成功获取了数据,并将数据中的图片加载到推送通知中,但我无法在推送通知窗口中显示该图片.请告诉我如何修复它? over
因此在 iOS 10 中,您现在可以编写“拦截”推送通知的通知服务扩展,以便您可以下载内容并在将它们显示给用户之前处理它们。 然而,他们要求推送有效负载将 aps 中的 'mutable-conten
是否有任何方法可以从 iOS 应用程序的 Notification-Service-Extension 实例内部获取保存在沙盒中的数据?我想在传递 contenhandler 之前从数据库中获取一些数
我正在尝试实现 UNNotificationServiceExtension,但我的代码似乎经常失败,只是说明 Program ended with exit code: 0" 我正在尝试在扩展中使用
我正在使用 UNNotificationServiceExtension 将通知中的电话号码替换为姓名。我正在尝试在 CNContactStore 中查找电话号码,并将 Ph# 替换为联系人姓名。 我
UNNotificationServiceExtension 和 UNNotificationContentExtension 之间有什么区别。 什么是一个人能做而另一个人不能做的事情。我已经阅读了文
我写了一个 UNNotificationServiceExtension,我试图在其中下载文件并附加它。每次我运行我都会得到一个异常说 Invalid attachment file URL. 我这辈
我正在使用 UNNotificationServiceExtension 在 iOS 10 上创建带有图像附件的丰富通知。一切都适用于较小的图像,除非要下载的图像稍大( e.g. 7MB )。 在这种
我的应用程序有一个 UNNotificationServiceExtension 对象,该对象存在于单独的项目中,用于处理通知的接收和处理。 我希望能够在某些情况下触发主应用程序中的事件,无论该应用程
当我的应用程序收到远程推送通知时,我需要调用后端服务,即使该应用程序已被终止。为此,我在“didReceive”方法中使用 UNNotificationServiceExtension 拦截通知,并在
我正在使用 UINotificationServiceExtension 成功修改我的远程通知负载。 我想根据用户在这里的选择更改提示音。为此,我需要将 UINotificationSound 对象分
我是一名优秀的程序员,十分优秀!