gpt4 book ai didi

通过 FCM 的 iOS 严重警报

转载 作者:行者123 更新时间:2023-12-01 15:57:46 25 4
gpt4 key购买 nike

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

最佳答案

回答我自己的问题。

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

关于通过 FCM 的 iOS 严重警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52658024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com