swift – Picture Push Notification Not Displaying in iOS with Notification Service Extension

0
23
swift – Picture Push Notification Not Displaying in iOS with Notification Service Extension


I am at present engaged on implementing picture push notifications in my iOS app utilizing a Notification Service Extension, however the picture just 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 #obtainable(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 {
    
    non-public var contentHandler: ((UNNotificationContent) -> Void)?
    non-public var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        os_log("Entered didReceive methodology.", log: .default, sort: .data)
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content material.mutableCopy() as? UNMutableNotificationContent)

        defer {
            os_log("Finishing content material handler.", log: .default, sort: .data)
            contentHandler(bestAttemptContent ?? request.content material)
        }

        guard let attachment = request.attachment else {
            os_log("Attachment not discovered.", log: .default, sort: .error)
            return
        }

        os_log("Attachment discovered: %@", log: .default, sort: .data, attachment.identifier)
        bestAttemptContent?.attachments = [attachment]
    }
    
    override func serviceExtensionTimeWillExpire() {
        os_log("Service extension time will expire.", log: .default, sort: .data)
        if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}

extension UNNotificationRequest {
    var attachment: UNNotificationAttachment? {
        os_log("Trying to fetch picture URL from payload.", log: .default, sort: .data)
        guard let attachmentURL = content material.userInfo["image_url"] as? String else {
            os_log("Picture URL not present in payload.", log: .default, sort: .error)
            return nil
        }

        let encodedURLString = attachmentURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        os_log("Authentic URL: %@", log: .default, sort: .data, attachmentURL)
        os_log("Encoded URL: %@", log: .default, sort: .data, encodedURLString ?? "nil")

        guard let encodedURL = URL(string: encodedURLString ?? ""), let imageData = strive? Information(contentsOf: encodedURL) else {
            os_log("Didn't create URL from encoded string.", log: .default, sort: .error)
            return nil
        }

        os_log("Creating attachment with fetched picture knowledge.", log: .default, sort: .data)
        return strive? UNNotificationAttachment(knowledge: imageData, choices: nil)
    }
}

extension UNNotificationAttachment {

    comfort init(knowledge: Information, 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 knowledge.write(to: fileURL)
        os_log("Picture knowledge written to file: %@", log: .default, sort: .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 just isn’t showing within the notification. I’ve ensured the picture URL is accessible and accurately formatted. Any help or insights on resolving this subject could be enormously appreciated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here