当我从服务器发送此有效负载时,会弹出正常通知。未调用扩展来获取丰富的通知。
使用 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)
}
}
最佳答案
有效负载结构应该是这样的
{
"aps":{
"alert":{
"title":"Push Pizza Co.",
"body":"Your pizza is almost ready!"
},
"badge”:1,
"sound":"default",
"category":"catName",
"mutable-content":1
},
"attachment-url": "https://www.example.com/image_url"
}
关于swift - ios13 中未调用丰富通知扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59091537/