通过FCM进行iOS严重警报

问题描述 投票:0回答:2

iOS 12添加了重要警报。 APNS有效负载具有sound dictionary以支持关键警报。 FCM有效负载中是否存在等效的声音字典支持,以便向iOS设备发送FCM通知。

ios firebase push-notification apple-push-notifications firebase-cloud-messaging
2个回答
1
投票

FCM目前没有声音字典支持,相当于iOS的声音字典。我确信你已经知道了,在声音方面,FCM和APNs的对手是sound参数:

设备收到通知时播放的声音。

声音文件可以位于客户端应用程序的主包中,也可以位于应用程序数据容器的Library / Sounds文件夹中。有关更多信息,请参阅iOS Developer Library

但是,从UNNotificationSound文档中读取,也许您可​​以尝试添加包含标识符(例如data)的"isCritical": "true"消息有效负载,然后让您的应用根据需要处理它。


1
投票

回答我自己的问题。

由于FCM有效负载不支持iOS声音字典,因此我不得不依赖通知扩展来实现魔力。我将“关键”标志设置为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)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.