iOS 12添加了严重警报。APNS有效负载具有支持严重警报的sound dictionary。在FCM有效负载中是否有等效的声音字典支持向iOS设备发送FCM通知。
发布于 2018-11-08 03:31:53
回答我自己的问题。
我不得不依靠通知扩展来实现这个魔术,因为FCM的有效负载不支持iOS声音字典。我将" critical“标志设置为1作为FCM数据有效负载的一部分,并在通知扩展中使用它将通知标记为关键。
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, let critical = userInfo["critical"] as? String, Int(critical)! == 1 {
//critical alert try to change the sound if sound file is sent in notificaiton.
if let sound = apsInfo["sound"] as? String {
//sound file is present in notification. use it for critical alert..
bestAttemptContent.sound =
UNNotificationSound.criticalSoundNamed(UNNotificationSoundName.init(sound),
withAudioVolume: 1.0)
} else {
//sound file not present in notifiation. use the default sound.
bestAttemptContent.sound =
UNNotificationSound.defaultCriticalSound(withAudioVolume: 1.0)
}
contentHandler(bestAttemptContent)
}
}
}发布于 2018-10-08 00:47:42
目前在FCM中没有声音字典支持,这相当于iOS的声音字典。我相信你已经知道了,当涉及到声音时,FCM与APNs的对应是sound参数:
设备收到通知时要播放的声音。
声音文件可以位于客户端应用程序的主包中,也可以位于应用程序数据容器的Library/ Sound文件夹中。有关详细信息,请参阅iOS Developer Library。
然而,从UNNotificationSound文档中读取,也许你可以尝试添加一个包含标识符(例如"isCritical": "true")的data消息有效负载,然后让你的应用程序根据需要处理它。
发布于 2021-05-26 19:59:33
firebase现在已经添加了严重警报。
可以像这样添加到MessagingPayload中:
const messagingPayload = {
token: this.FCMToken,
notification: {
...payload,
},
apns: {
payload: {
aps: {
criticalSound: {
critical: true,
name: 'default',
volume: 1.0,
},
},
},
},
};
return admin.messaging().send(messagingPayload);文档可能有点令人困惑。您必须使用messaging().send()并在有效负载中对令牌进行编码,而不是使用messaging().sendToDevice()。
有效负载消息是TokenMessage https://firebase.google.com/docs/reference/admin/node/admin.messaging.TokenMessage
附注:
你还需要从苹果获得授权,然后才能使用严重警报:https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/
https://stackoverflow.com/questions/52658024
复制相似问题