"aps": {
"alert": {
"title": "Hello",
"body": "What do you think of this image?"
},
"mutable_content": true
},
"media-url": "https://firebasestorage.googleapis.com/....."
//
// NotificationService.swift
// RichNotification
//
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
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)
guard let bestAttemptContent = bestAttemptContent,
let attachmentURLAsString = bestAttemptContent.userInfo["image"] as? String,
let attachmentURL = URL(string: attachmentURLAsString) else {
return
}
downloadImageFrom(url: attachmentURL) { (attachment) in
if let attachment = attachment {
bestAttemptContent.attachments = [attachment]
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)
}
}
private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void) {
let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
// 1. Test URL and escape if URL not OK
guard let downloadedUrl = downloadedUrl else {
completionHandler (nil)
return
}
// 2. Get current's user temporary directory path
var urlPath = URL(fileURLWithPath: NSTemporaryDirectory ())
// 3. Add proper ending to url path, in the case jpg (The system validates the content of attached files before scheduling the corresponding notification request. If an attached file is corrupted,
let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".jpg"
urlPath = urlPath.appendingPathComponent (uniqueURLEnding)
// 4. Move downloadedUrl to newly created urlPath
try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath)
// 5. Try adding getting the attachment and pass it to the completion handler
do {
let attachment = try UNNotificationAttachment (identifier: "picture", url: urlPath, options: nil)
completionHandler(attachment)
}
catch {
completionHandler(nil)
}
}
task.resume()
}
}
extension UNNotificationRequest {
var attachment: UNNotificationAttachment? {
guard let attachmentURL = content.userInfo["image"] as? String, let imageData = try? Data(contentsOf: URL(string: attachmentURL)!) else {
return nil
}
return try? UNNotificationAttachment(data: imageData, options: nil)
}
}
extension UNNotificationAttachment {
convenience init(data: Data, options: [NSObject: AnyObject]?) throws {
let fileManager = FileManager.default
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true)
try fileManager.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil)
let imageFileIdentifier = UUID().uuidString + ".jpg"
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL)
try self.init(identifier: imageFileIdentifier, url: fileURL, options: options)
}
}
4条答案
按热度按时间euoag5mw1#
我有同样的问题,为什么丰富的通知不工作..和问题是与FCM
mutable-content
有效负载..它变成gcm.notification.mutable-content
时,有效负载接收,这就是为什么有一个问题尚未解决。有关详细信息,您可以查看此issue from github.
现在问题解决了。您可以检查该链接,也可以检查此answer AL。
rur96b6h2#
只是更新了@ElCaptainv2.0的答案。
为FCM添加了
mutable_content
:当前仅适用于iOS 10+设备。在iOS上,使用此字段表示APNS有效负载中的可变内容。发送通知且此字段设置为true时,可以使用Notification Service app extension在显示之前修改通知内容。对于Android和Web,将忽略此参数。
注意:参数值应为
boolean
,而不是Integer
。就在几个小时前,kroikie在GitHub thread中也提到了这一点。
nfzehxib3#
我花了好几个小时,好几个小时,好几个小时,我所有的通知都运行良好,除了
rich notification
图像永远不会显示。下面是简单英语:更改此内容:
"mutable-content": 1
//错误对此:
"mutable_content": true
//正确以下是传出的有效负载:
yqhsw0fo4#
以下内容对我有效(2023年2月)
步骤1 -添加通知服务扩展
第2步-将目标添加到Podfile
步骤3 -使用扩展帮助程序
转换为Swift:
通过Firebase/FCM发送推送通知的cURL