I am stuck in displaying image in ios push notification in ionic4.I am not getting where issue is.I tried with notification service extension and also content extension.but still image is not display in notification.It is working perfect in Android.
This is notificationService.swift file:
import UserNotifications
import MobileCoreServices
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)
if let bestAttemptContent = bestAttemptContent {
if let data = bestAttemptContent.userInfo["fcm_options"] as? [String:Any],
let attachmentString = data["image"] as? String,
let attachmentUrl = URL(string:attachmentString){
let session = URLSession(configuration: URLSessionConfiguration.default)
let downloadTask = session.downloadTask(with:attachmentUrl, completionHandler:{(url, _,error) in
if error != nil{
print("Error downloading attachment: \(error!.localizedDescription)")
}else if let url = url{
let attachment = try!UNNotificationAttachment(identifier: attachmentString, url: url, options: [UNNotificationAttachmentOptionsTypeHintKey : kUTTypePNG])
bestAttemptContent.attachments = [attachment]
}
contentHandler(bestAttemptContent)
})
downloadTask.resume()
}
}
}
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)
}
}
}
This is notificationViewController.swift File:
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var label: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any required interface initialization here.
}
func didReceive(_ notification: UNNotification) {
print("content",notification);
self.label?.text = notification.request.content.body
}
}
This is info.plist of content extension.
This is my payload for notification:
{
"google.c.a.ts":"1574236630",
"gcm.notification.sound2":"default",
"google.c.a.e":"1",
"fcm_options":{
"image":"https:\/\/qph.fs.quoracdn.net\/main-qimg-
2409a441a71fdc85cf987c349d364cb4.webp"},
"aps":{
"alert":{
"title":"testing",
"body":"testing...."
},
"sound":"default",
"mutable-content":"1"
},
"gcm.n.e":"1",
"google.c.a.c_id":"6815041840540114734",
"google.c.a.udt":"0",
"content-available":"1",
"mutable-content":"1",
"gcm.message_id":"1574236630701793"
}
Please help me..,Thank you in advance.
When you using fcm notification then use mutable_content:true
instead of mutable-content = 1