gpt4 book ai didi

ios - 通知托盘中带有扩展图像的丰富通知

转载 作者:行者123 更新时间:2023-12-03 17:39:18 24 4
gpt4 key购买 nike

我正在尝试使用这样的扩展图像在我的应用程序上显示丰富的通知。我已经使用通知服务扩展在应用程序中实现了这一点。 enter image description here

但是当我收到通知时,我只会得到一个缩略图,看起来像这样。当我长按支持 3D 触摸功能的手机时会出现扩展图像,否则它只会在没有 3D 触摸功能的手机上显示缩略图。

enter image description here

我无法在 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
}
}

最佳答案

这是一个非常古老的问题,我想您现在已经发现,您要实现的目标在技术上是不可行的。
通知以折叠形式显示,并且仅当用户 3d 按下(或在没有 3d 触摸的设备的情况下长按)通知时,它们才会以展开形式显示。

关于ios - 通知托盘中带有扩展图像的丰富通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48921706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com