ios - UNNotificationServiceExtension 不适用于 Firebase Cloud Functions

标签 ios swift push-notification firebase-cloud-messaging google-cloud-functions

我将 UNNotificationServiceExtension 集成到应用程序中,但我没有图像等附加内容。我还认为我没有正确实现图片的加载,因此我尝试更改测试的名称和正文负载。但它没有给我任何结果。我通过 Firebase 控制台和 Firebase 云功能实现推送通知。我还阅读了有关此主题的几篇文章,但它们对我没有帮助。请告诉我如何解决?

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 {
            // Modify the notification content here...
            bestAttemptContent.title = "Apple [modified]"
            bestAttemptContent.body = "Xcode"
            let attachmentStorage = AttachmentStorage()

            if let jpeg = request.content.userInfo["image"] as? String {
                guard let url = URL(string: jpeg) else {
                    contentHandler(bestAttemptContent)
                    return
                }
                debugPrint("url", url)
                attachmentStorage.store(url: url, extension: "jpeg") { (path, error) in
                    if let path = path {
                        do {
                            let attachment = try UNNotificationAttachment(identifier: "image", url: path, options: nil)
                            bestAttemptContent.attachments = [attachment]
                            contentHandler(bestAttemptContent)
                            return
                        } catch {
                            contentHandler(bestAttemptContent)
                            return
                        }
                    }
                }
            }

            if let png = request.content.userInfo["png"] as? String {
                guard let url = URL(string: png) else {
                    contentHandler(bestAttemptContent)
                    return
                }
                attachmentStorage.store(url: url, extension: "png") { (path, error) in
                    if let path = path {
                        do {
                            let attachment = try UNNotificationAttachment(identifier: "image", url: path, options: nil)
                            bestAttemptContent.attachments = [attachment]
                            contentHandler(bestAttemptContent)
                            return
                        } catch {
                            contentHandler(bestAttemptContent)
                            return
                        }
                    }
                }
            }

            contentHandler(bestAttemptContent)
        }
    }

    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)
        }
    }

}

class AttachmentStorage {

    func store(url: URL, extension: String, completion: ((URL?, Error?) -> ())?) {
        // obtain path to temporary file
        let filename = ProcessInfo.processInfo.globallyUniqueString

        let path = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(filename).\(`extension`)")

        // fetch attachment
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            let _ = try! data?.write(to: path)
            completion?(path, error)
        }
        task.resume()
    }

}

来自云函数的负载

   const payload = {
            notification: {
                title: 'It’s decision time!',
                body: 'text'
                sound: 'default',
                mutable_content: true
            },
            data: {
                image: "https://example.com/static_logos/320x320.png"
            }
        };

Push notification from firebase console

更新:我也读过这篇文章issue

最佳答案

我修好了。

我发送了 mutable_content 作为 true。但奇怪的是,Firebase 工程师期望的是一个字符串,即“true”而不是

const payload = {
                    notification: {
                        title: 'It’s decision time!',
                        body: "text"
                        sound: 'default',
                        mutable_content: 'true'
                    },
                    data: {
                        image: "example"
                    }
                };

关于ios - UNNotificationServiceExtension 不适用于 Firebase Cloud Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996156/

相关文章:

ios - 如何快速显示来自API的图像?

ios - 如何在 Swift 3 中居中和纵横比适合图像?

ios - Urban Airship 的推送通知角标(Badge)不会在后台更新

python-2.7 - 如果使用 python 和 opencv 在两个目录之间检测到公共(public)面孔,如何通知用户

iphone - 在应用程序终止时生成通知 iOS

ios - 在最初被拒绝后,我如何获得隐私权限以重新出现?

ios - 如何将 UISegmentedControl 与 UITableViewController 结合使用?

ios - 使用 QuickTime 从 iPhone 捕获屏幕会禁用设备上的声音

ios - 从 Alamofire 上传图像中提取响应消息

ios - UITableView 中最后一个单元格下方有灰色