【问题标题】:Rich Notification extension not called in ios13ios13中未调用丰富的通知扩展
【发布时间】: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


    【解决方案1】:

    有效载荷结构应该是这样的

    {
        "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"
    
    }
    

    【讨论】:

    • 如果我尝试使用aps 未收到通知
    【解决方案2】:

    您应该为 iOS 自定义消息。来自 Firebase 文档:

    {
      "message":{
        "token" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "notification" : {
          "body" : "This is an FCM notification that displays an image.!",
          "title" : "FCM Notification",
         },
         "apns": {
             "payload": {
                 "aps": {
                     "mutable-content": 1
                 }
             },
             "fcm_options": {
                 "image": "url-to-image"
             }
           }
       }
    }
    

    https://firebase.google.com/docs/cloud-messaging/ios/send-image

    【讨论】:

      猜你喜欢
      • 2018-08-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多