通过FCM的iOS严重警报 [英] iOS critical alert through FCM

查看:183
本文介绍了通过FCM的iOS严重警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 12添加了严重警报. APNS有效负载具有声音字典以支持关键警报. FCM有效载荷中是否有等效的声音词典支持,可将FCM通知发送到iOS设备.

iOS 12 has added critical alerts. The APNS payload has the sound dictionary to support critical alerts. Is there an equivalent sound dictionary support in FCM payload to send FCM notifications to iOS devices.

推荐答案

回答我自己的问题.

由于FCM有效负载不支持iOS声音字典,因此我不得不依靠通知扩展来完成操作.我将FCM数据有效载荷的一部分中的关键"标志发送为1,并在通知扩展中使用它,以将通知标记为关键.

I had to rely on notification extension to do the magic as the FCM payload doesn't support the iOS sound dictionary. I send the "critical" flag set to 1 as part of FCM data payload and use it in notification extension to mark a notification as critical.

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)
        }
    }
}

这篇关于通过FCM的iOS严重警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆