Firebase FCM消息支持Android的tag
,这会导致一条新通知,将旧通知替换为具有相同标签的旧通知。有什么办法可以为ios做同样的事情吗?
此answer建议在数据有效负载中使用线程ID。但这对我没有用。
最佳答案
旧版FCM HTTP服务器协议中没有thread-id
FCM有效负载密钥。解决方法是使用iOS Notification Service Extension。发送分组标识符作为FCM数据有效载荷中的“线程ID”键,并让服务扩展读取“线程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)
}
}
}