swift - ios13 中未调用丰富通知扩展

标签 swift iphone push-notification firebase-cloud-messaging ios13

当我从服务器发送此有效负载时,会弹出正常通知。未调用扩展来获取丰富的通知。

使用 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/

相关文章:

ios - 首次启动应用程序时调用 didReceiveRemoteNotification

swift - 如何检测来自 NSWindowController 内的 NSToolbarItem 的点击?

iphone - IOS 应用 UIWebView 或导航 Controller

ios - 从字符串 URL 数组下载图像并保存到文件?

iOS 将 MPMoviePlayerController 镜像到电视,全屏播放电视屏幕

iphone - 对于旋转的 UIImageView 有什么快速而肮脏的抗锯齿技术吗?

android - 具有相同 Intent 的推送通知打开 Activity

javascript - 如何使本地 APNS 推送通知静音?

ios - UIAccessibilityContainer 动态更新协议(protocol)方法

ios - Swift - 核心数据被覆盖