Flutter iOS Rich推送通知图像未显示



Flutter推送通知

我实现了推送通知,通知在Flutter中有图像链接。在安卓系统中,图像会自动显示在通知中心,并带有通知详细信息。在iOS中,图像不显示在通知中心,而仅显示通知中心的通知详细信息

请参阅推送通知的格式。卷曲通过,Php开发人员正在发送通知。我成功收到通知,但图像没有在iOS端自动显示,在Android中图像会自动显示我已经添加了";可变内容":是的,已经尝试添加";可变内容":在通知块中为true,但未成功

{
"to": "device token",
"mutable-content": true,
"notification": {
"title": "Note 3",
"body": "11:41",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"image": "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"

},
"data": {
"notification_type": "Nice Thoughts",
"post_details": {
"message": "hello",
"color": "#e0e0e0",
"url": "www.recurpost.com",
"img_url": "https://homepages.cae.wisc.edu/~ece533/images/airplane.png",
"video_url": "",
"name": "abc xyz"
}
},
"priority": "high"

}

您需要创建一个通知服务扩展,如所述

以下步骤对我有效:

步骤1-添加通知服务扩展

步骤2-将目标添加到Podfile

步骤3-使用扩展助手

Swift:中的NotificationService.swift

//
//  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)
}
}

cURL通过Firebase/FCM 发送推送通知

curl --location --request POST 'https://fcm.googleapis.com/fcm/send' 
--header 'Authorization: key=<YOUR FCM KEY>' 
--header 'Content-Type: application/json' 
--data-raw '{
"to": "fcm token for device or channel id",
"content_available": true,
"mutable_content": true,
"notification": {
"title": "Notification With Image",
"mutable-content": true,
"body": "Test Message "
}, 
"data": {
"image": "https://upload.wikimedia.org/wikipedia/commons/1/16/HDRI_Sample_Scene_Balls_%28JPEG-HDR%29.jpg"
}
}'

最新更新