I am presently engaged on implementing picture push notifications in my iOS app utilizing a Notification Service Extension, however the picture isn’t displaying within the notification. Beneath are the small print of my implementation:
import UIKit
import Flutter
import GoogleMaps
import Firebase
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func utility(
_ utility: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("AIzaSyCW7N3YiGrlsdDPsXLeZ6Uo5HDQwAgqGLw")
FirebaseApp.configure()
UNUserNotificationCenter.present().delegate = self
if #out there(iOS 12.0, *) {
UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .badge, .sound]) { granted, error in
print("Permission granted: (granted)")
}
}
utility.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return tremendous.utility(utility, didFinishLaunchingWithOptions: launchOptions)
}
}
NotificationService:
import UserNotifications
import os.log
class NotificationService: UNNotificationServiceExtension {
personal var contentHandler: ((UNNotificationContent) -> Void)?
personal var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
os_log("Entered didReceive methodology.", log: .default, kind: .data)
self.contentHandler = contentHandler
bestAttemptContent = (request.content material.mutableCopy() as? UNMutableNotificationContent)
defer {
os_log("Finishing content material handler.", log: .default, kind: .data)
contentHandler(bestAttemptContent ?? request.content material)
}
guard let attachment = request.attachment else {
os_log("Attachment not discovered.", log: .default, kind: .error)
return
}
os_log("Attachment discovered: %@", log: .default, kind: .data, attachment.identifier)
bestAttemptContent?.attachments = [attachment]
}
override func serviceExtensionTimeWillExpire() {
os_log("Service extension time will expire.", log: .default, kind: .data)
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationRequest {
var attachment: UNNotificationAttachment? {
os_log("Making an attempt to fetch picture URL from payload.", log: .default, kind: .data)
guard let attachmentURL = content material.userInfo["image_url"] as? String else {
os_log("Picture URL not present in payload.", log: .default, kind: .error)
return nil
}
let encodedURLString = attachmentURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
os_log("Unique URL: %@", log: .default, kind: .data, attachmentURL)
os_log("Encoded URL: %@", log: .default, kind: .data, encodedURLString ?? "nil")
guard let encodedURL = URL(string: encodedURLString ?? ""), let imageData = strive? Knowledge(contentsOf: encodedURL) else {
os_log("Did not create URL from encoded string.", log: .default, kind: .error)
return nil
}
os_log("Creating attachment with fetched picture information.", log: .default, kind: .data)
return strive? UNNotificationAttachment(information: imageData, choices: nil)
}
}
extension UNNotificationAttachment {
comfort init(information: Knowledge, choices: [NSObject: AnyObject]?) throws {
let fileManager = FileManager.default
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true)
strive fileManager.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil)
let imageFileIdentifier = UUID().uuidString + ".jpg"
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
strive information.write(to: fileURL)
os_log("Picture information written to file: %@", log: .default, kind: .data, fileURL.absoluteString)
strive self.init(identifier: imageFileIdentifier, url: fileURL, choices: choices)
}
}
data.plist:
NSAppTransportSecurity
NSAllowsArbitraryLoads
playload has mutable-content: 1
,
Regardless of the above configurations, the picture isn’t showing within the notification. I’ve ensured the picture URL is accessible and appropriately formatted. Any help or insights on resolving this subject could be significantly appreciated.