你可以为ios设置带有FCM通知的thread-id吗

Ari*_*Ari 2 ios firebase google-cloud-messaging firebase-cloud-messaging cordova-plugin-fcm

Firebase FCM 消息支持tagAndroid,这会导致新通知用具有相同标记的旧通知替换前一个通知。有没有办法在ios上做同样的事情?

这个答案建议在数据有效负载中使用线程 ID。但这对我不起作用。

Sha*_*ani 5

旧版 FCM HTTP 服务器协议中没有thread-idFCM 有效负载密钥。解决方法是使用 iOS 通知服务扩展。将分组标识符作为 FCM 数据负载中的“thread-id”键发送,并让服务扩展读取“thread-id”键并将其设置为通知内容的threadIdentifier属性。

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)
        let userInfo: [AnyHashable : Any] = (bestAttemptContent?.userInfo)!  
        if let apsInfo = userInfo["aps"] as? [AnyHashable: Any], let bestAttemptContent = bestAttemptContent  {                
            //add thread-id to group notification.
            let patientId = userInfo["thread-id"] as! String
            bestAttemptContent.threadIdentifier = patientId
            contentHandler(bestAttemptContent)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你能解释一下我应该在哪里添加这段代码吗?我正在使用 Ionic/Cordova。 (2认同)