UNNotificationServiceExtension不适用于Firebase云功能

时间:2017-05-16 08:26:40

标签: 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

1 个答案:

答案 0 :(得分:1)

我修好了。我无法假设Firebase工程师在字符串中是真的......

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