【发布时间】:2020-03-24 06:27:27
【问题描述】:
当我从服务器发送此有效负载时,会弹出正常通知。扩展不调用丰富的通知。
使用 Firebase 服务
1) 在 AppDelegate 中注册的类别 2) 在有效载荷中添加“mutable_content = true” 3)在有效载荷中尝试了类别名称 4) 选择扩展作为应用调试的目标 5) 在扩展的 info.plist 中添加了允许任意加载 true。 帮帮我!!
通知负载:
{
"to": "FCM token",
"mutable_content": true,
"notification":
{
"title":"Offers",
"body":"Buy one Get One",
"badge":1,
"sound":"default"
},
"data": {
"attachment-url": "https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/SOME_LIKE_IT_HOT_TITLE.jpg/800px-SOME_LIKE_IT_HOT_TITLE.jpg"
}
}
自定义类
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
private var contentHandler: ((UNNotificationContent) -> Void)?
private var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
defer {
contentHandler(bestAttemptContent ?? request.content)
}
guard let attachment = request.attachment else { return }
bestAttemptContent?.attachments = [attachment]
}
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 UNNotificationRequest {
var attachment: UNNotificationAttachment? {
guard let attachmentURL = content.userInfo["attachment-url"] as? String, let imageData = try? Data(contentsOf: URL(string: attachmentURL)!) else {
return nil
}
return try? UNNotificationAttachment(data: imageData, options: nil)
}
}
extension UNNotificationAttachment {
convenience init(data: Data, options: [NSObject: AnyObject]?) throws {
let fileManager = FileManager.default
let temporaryFolderName = ProcessInfo.processInfo.globallyUniqueString
let temporaryFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(temporaryFolderName, isDirectory: true)
try fileManager.createDirectory(at: temporaryFolderURL, withIntermediateDirectories: true, attributes: nil)
let imageFileIdentifier = UUID().uuidString + ".png"
let fileURL = temporaryFolderURL.appendingPathComponent(imageFileIdentifier)
try data.write(to: fileURL)
try self.init(identifier: imageFileIdentifier, url: fileURL, options: options)
}
}
【问题讨论】:
标签: swift iphone push-notification firebase-cloud-messaging ios13