2

我被困在 ionic4 的 ios 推送通知中显示图像。我没有找到问题所在。我尝试了通知服务扩展和内容扩展。但通知中没有显示静止图像。它在 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"
}

请帮助我..,提前谢谢你。

4

1 回答 1

1

当您使用 fcm 通知时,请使用mutable_content:true而不是mutable-content = 1

于 2019-11-30T05:35:20.810 回答