我被困在ios中显示图像,在离子4推送通知。我不知道问题在哪里,我尝试了通知服务扩展和内容extension.but仍然没有显示在notification.It上是完美的工作在安卓系统。
这是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"
}请帮我.,谢谢你提前。
发布于 2019-11-30 05:35:20
当您使用fcm通知时,然后使用mutable_content:true而不是mutable-content = 1
https://stackoverflow.com/questions/58949434
复制相似问题