I am having bother displaying a picture in push notifications. Initially, my “identifier” was not distinctive, which posed the chance of my “attachments” being overwritten. I’ve now made the identifier distinctive, however consequently, the picture not reveals up in push notifications. Any help could be vastly appreciated. Additionally I did create a Notification Service Extension and in addition I been utilizing Postman to push the notifications to my precise gadget utilizing google firebase.
View Controller
import UserNotifications
import UIKit
import UserNotificationsUI
class ImagePushNotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
tremendous.viewDidLoad()
}
// The code first checks if the notification content material has an attachment.
func didReceive(_ notification: UNNotification) {
let content material = notification.request.content material
if let attachment = content material.attachments.first,
attachment.url.startAccessingSecurityScopedResource(),
let imageData = attempt? Knowledge(contentsOf: attachment.url),
let picture = UIImage(knowledge: imageData) {
self.imageView.picture = picture
attachment.url.stopAccessingSecurityScopedResource()
}
}
}
NotificationService
import UserNotifications
protocol ImageLoader {
func downloadImageData(from url: URL, completion: @escaping (Knowledge?) -> Void)
}
class DefaultImageLoader: ImageLoader {
func downloadImageData(from url: URL, completion: @escaping (Knowledge?) -> Void) {
URLSession.shared.dataTask(with: url) { knowledge, _, _ in
DispatchQueue.major.async {
completion(knowledge)
}
}.resume()
}
}
class NotificationModifier {
let imageLoader: ImageLoader
init(imageLoader: ImageLoader) {
self.imageLoader = imageLoader
}
func addAttachment(_ content material: UNMutableNotificationContent, contentHandler: @escaping (UNNotificationContent) -> Void) {
guard let urlString = content material.userInfo["image"] as? String,
let fileUrl = URL(string: urlString)
else {
contentHandler(content material)
return
}
downloadImageData(from: fileUrl) { imageData in
if let knowledge = imageData {
let fileIdentifier = "picture.jpg"
if let attachment = UNNotificationAttachment.saveImageToDisk(
fileIdentifier: fileIdentifier, knowledge: knowledge, choices: nil
) {
content material.attachments = [attachment]
content material.title = "(content material.title)"
// Delete the attachment file
do {
attempt FileManager.default.removeItem(at: attachment.url)
} catch {
print("Did not delete attachment file: (error)")
}
}
}
contentHandler(content material)
}
}
func downloadImageData(from url: URL, completion: @escaping (Knowledge?) -> Void) {
imageLoader.downloadImageData(from: url, completion: completion)
}
}
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 material.mutableCopy() as? UNMutableNotificationContent)
let imageLoader = DefaultImageLoader()
let notificationModifier = NotificationModifier(imageLoader: imageLoader)
if let bestAttemptContent = bestAttemptContent {
notificationModifier.addAttachment(bestAttemptContent, contentHandler: contentHandler)
} else {
if let content material = request.content material.copy() as? UNNotificationContent {
contentHandler(content material)
}
}
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationAttachment {
static func saveImageToDisk(fileIdentifier: String, knowledge: Knowledge, choices: [NSObject: AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = fileManager.temporaryDirectory.appendingPathComponent(folderName, isDirectory: true)
do {
attempt fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
let uniqueIdentifier = "(fileIdentifier)_(knowledge.hashValue)"
let fileURL = folderURL.appendingPathComponent(uniqueIdentifier)
attempt knowledge.write(to: fileURL, choices: [])
let attachment = attempt UNNotificationAttachment(identifier: uniqueIdentifier, url: fileURL, choices: choices)
return attachment
} catch {
print("error: (error)")
}
return nil
}
}