[ad_1]
Hi I need to ship Firebase push Notification with Picture in swift.At the moment I get textual content message from Firebase.However Whereas testing on Android telephone I get Picture push notification from Firebase Console.
I had included notification service extension from goal and I activated the service when the immediate Appeared.The notification service code is beneath.
Thanks in Advance
import UserNotifications
import FirebaseMessaging
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)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content material right here...
bestAttemptContent.title = "(bestAttemptContent.title) [modified]"
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Referred to as simply earlier than the extension will probably be terminated by the system.
// Use this as a chance to ship your "finest try" at modified content material, in any other case the unique push payload will probably be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
extension UNNotificationRequest {
var attachment: UNNotificationAttachment? {
guard let attachmentURL = content material.userInfo["image_url"] as? String, let imageData = attempt? Information(contentsOf: URL(string: attachmentURL)!) else {
return nil
}
return attempt? UNNotificationAttachment(information: imageData, choices: nil)
}
}
extension UNNotificationAttachment {
comfort init(information: Information, choices: [NSObject: AnyObject]?) throws {
let fileManager = FileManager.default
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true)
attempt fileManager.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil)
let imageFileIdentifier = UUID().uuidString + ".jpg"
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
attempt information.write(to: fileURL)
attempt self.init(identifier: imageFileIdentifier, url: fileURL, choices: choices)
}
}
[ad_2]
