【发布时间】:2018-08-01 23:11:48
【问题描述】:
我正在尝试在我的应用上显示丰富的通知,并使用这样的扩展图像。我已使用通知服务扩展在应用程序中实现此功能。
但是当我收到通知时,我只会得到一个缩略图,看起来像这样。当我长按支持 3D-touch 的手机时会出现展开的图像,否则它只会在不支持 3D-touch 的手机上显示缩略图。
我找不到任何关于 SO 的文档或任何问题,如果可能的话,它解释了如何执行此操作。我想知道是否可以在 iOS 上执行此操作,如果没有,是否有任何可能的解决方法来完成此操作?这是我的NotificationSerivce 分机。任何帮助深表感谢!谢谢!
class NotificationService: UNNotificationServiceExtension {
let fileManager = FileManager()
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
return self.contentHandler = contentHandler
}
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
guard let attachmentURL = content.userInfo["attachment-url"] as? String else {
return self.contentHandler = contentHandler
}
guard let fileName = attachmentURL.components(separatedBy: "/").last else {
return self.contentHandler = contentHandler
}
guard let imageData = try? Data(contentsOf: URL(string: attachmentURL)!) else {
return self.contentHandler = contentHandler
}
if let thumbnailAttachment = UNNotificationAttachment.create(imageFileIdentifier: fileName, data: imageData, options: nil) {
bestAttemptContent.attachments = [thumbnailAttachment]
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationAttachment {
/// Save the image to disk
static func create(imageFileIdentifier: String, data: Data, options: [AnyHashable: Any]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString
let tmpSubFolderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true)
do {
try fileManager.createDirectory(at: tmpSubFolderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = tmpSubFolderURL?.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL!, options: [])
let imageAttachment = try UNNotificationAttachment(identifier: imageFileIdentifier, url: fileURL!, options: options)
return imageAttachment
} catch let error {
print("error \(error)")
}
return nil
}
}
【问题讨论】:
-
找到解决办法了吗?
-
来自docs on
Data: "不要使用这个同步初始化器来请求基于网络的URL。对于基于网络的URL,这种方法可以在慢速网络上阻塞当前线程数十秒,导致糟糕的用户体验,并且在 iOS 中,可能会导致您的应用程序被终止。”这可能会导致您的服务扩展超时。您应该改用dataTask或downloadTask。使用Data进行网络调用的唯一有效用例是命令行工具
标签: ios swift unnotificationattachment