首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Swift使用Firebase云消息发送富推送通知

iOS Swift使用Firebase云消息发送富推送通知
EN

Stack Overflow用户
提问于 2020-07-07 01:59:45
回答 1查看 328关注 0票数 1

我无法使用镜像获取Firebase推送通知。我正在通过FCM接口发送有效载荷。当我使用图像的链接填写"Notification Image“字段时,该图像在我的设备上测试时无法通过。

推送通知已成功发送到我的手机,但图像从未出现。

我已经设置了通知服务扩展。任何帮助都是非常感谢的。

代码语言:javascript
复制
import UserNotifications
import FirebaseMessaging
import Foundation

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)
    
    guard let bestAttemptContent = bestAttemptContent,
        let attachmentUrlAsString = bestAttemptContent.userInfo["url"] as? String,
        let attachmentUrl = URL(string: attachmentUrlAsString) else {
            return
    }
    let mediaInfo = bestAttemptContent.userInfo
    Messaging.serviceExtension().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler)
    print("media info for push is: \(mediaInfo)")
    downloadImageFrom(url: attachmentUrl) { (attachment) in
        if let attachment = attachment {
            bestAttemptContent.attachments = [attachment]
            contentHandler(bestAttemptContent)
            Messaging.serviceExtension().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler)
        }
    }
}

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

}

extension NotificationService {
private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void){
    let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
        //1. Test url and escape if url has problem
        guard let downloadedUrl = downloadedUrl else {
            completionHandler(nil)
            return
        }
        
        //2.
        var urlPath = URL(fileURLWithPath: NSTemporaryDirectory())
        let uniqueUrlEnding = ProcessInfo.processInfo.globallyUniqueString + ".jpg"
        urlPath = urlPath.appendingPathComponent(uniqueUrlEnding)
        
        try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath)
        
        do {
            let attachment = try UNNotificationAttachment(identifier: "picture", url: urlPath, options: nil)
            completionHandler(attachment)
        } catch {
            completionHandler(nil)
        }
        
    }
    task.resume()
}
}

EN

回答 1

Stack Overflow用户

发布于 2020-10-05 23:30:55

我是这样解决的,适用于Firebase:

代码语言:javascript
复制
    guard let bestAttemptContent = bestAttemptContent,
        let fcmOptions = bestAttemptContent.userInfo["fcm_options"] as? [String: Any],
        let attachmentUrlAsString = fcmOptions["image"] as? String,
        let attachmentUrl = URL(string: attachmentUrlAsString) else {
            return
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62761830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档