iOS 使用 azure 通知中心推送通知声音停止工作 [根本没有声音]

iOS push notification sound stopped working using azure notification hub [ no sound at all ]

我的 ios 应用已经停止播放推送通知的声音很久了。我们有 azure 通知中心作为发送通知的后端。

根据我们与 MS Azure 团队的讨论,他们告知,为了启用声音,我们需要按照新的 API 包含“声音”属性。但是使用 azure 通知中心这个 API 会变成错误的请求,因为它们不支持“声音”属性。天蓝色的通知端无法执行任何其他操作,他们建议联系 APNS 寻求任何替代方案(如果有)。

他们仍在努力在 APNS 上添加对严重警报的支持。

有什么解决办法吗?有人遇到过这样的问题吗?任何帮助将不胜感激。

下面是注册推送通知的代码:

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound], completionHandler: {(_ granted: Bool, _ error: Error?) -> Void in
        if error == nil {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    })

要播放默认系统声音,请使用默认方法创建您的声音对象。

默认情况下,通知包含向用户显示的警告消息,但不会播放声音。如果您想在收到通知时播放声音,Apple 提供了一种您可以指定的“默认”声音。

{"aps":{"声音":"默认"}}

参考资料: https://developer.apple.com/documentation/usernotifications/unnotificationsound

有一个选项可以在本地向您的远程通知添加声音,这样就不需要依赖服务器来添加声音了属性,

You can also use a notification service app extension to add a sound file to a notification shortly before delivery. In your extension, create a UNNotificationSound object and add it to your notification content in the same way that you’d for a local notification.

为此需要创建一个新的目标,您可能需要为与主应用相同的通知扩展包 ID 创建 cer。

示例 UNNotificationServiceExtension 代码供您参考,

import UserNotifications

class NotificationService: UNNotificationServiceExtension {
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    
    // MARK:- Notification lifecycle
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        // -------------
        // To play default sound
        let defaultSound  = UNNotificationSound.default
        bestAttemptContent?.sound = defaultSound
        // -----OR------
        // To play custom sound
        let customSound  = UNNotificationSound(named: UNNotificationSoundName(rawValue: "somelocalfile"))
        bestAttemptContent?.sound = customSound
        // --------------
        contentHandler(bestAttemptContent!)
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your “best attempt” at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
            // If notification doesnt process in time, Increment badge counter. Default notification will be shown
            let customSound  = UNNotificationSound(named: UNNotificationSoundName(rawValue: "somelocalfile"))
            bestAttemptContent.sound = customSound
            contentHandler(bestAttemptContent)
        }
    }
}

注意: 根据 this 文档声音应作为 default 或任何 custom sound file path 发送。没有提及在有效负载中忽略“声音”的行为,认为这是强制性的 Sound!!.

编辑: @shaqir 来自 Apple documentation here

Include this key when you want the system to play a sound.

If the sound file cannot be found, or if you specify default for the value, the system plays the default alert sound.

表示如果payload中没有sound key,则不会播放声音。应该是 defaultwrong audio path.