图片未显示在 ionic ios 推送通知中
Image is not displayed in ionic ios push notification
我卡在 ios 推送通知中显示图像 ionic4.I 我没有找到问题 is.I 尝试使用通知服务扩展和内容 extension.but 静止图像在 notification.It 中不显示 在 Android 中工作完美。
这是notificationService.swift文件:
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)
}
}
}
这是notificationViewController.swift文件:
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
}
}
这是info.plist的内容扩展。
这是我的通知负载:
{
"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"
}
请帮助我..,提前谢谢你。
当您使用 fcm 通知时,请使用 mutable_content:true
而不是 mutable-content = 1
我卡在 ios 推送通知中显示图像 ionic4.I 我没有找到问题 is.I 尝试使用通知服务扩展和内容 extension.but 静止图像在 notification.It 中不显示 在 Android 中工作完美。
这是notificationService.swift文件:
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)
}
}
}
这是notificationViewController.swift文件:
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
}
}
这是info.plist的内容扩展。
这是我的通知负载:
{
"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"
}
请帮助我..,提前谢谢你。
当您使用 fcm 通知时,请使用 mutable_content:true
而不是 mutable-content = 1