iOS使用Azure通知中心推送通知时,通知声音停止工作 [没有任何声音]。

4

我的iOS应用程序已经很长时间停止播放推送通知的声音。我们使用Azure Notification Hub作为后端来发送通知。

根据我们与MS Azure团队的讨论,他们告诉我们,为了启用声音,我们需要按照新的API包括“Sound”属性。但是使用Azure Notification Hub,此API将变得不良请求,因为他们不支持“Sound”属性。从Azure Notification方面无法执行任何其他操作,并建议联系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 提供了一种名为“default”的声音可以指定使用。

{"aps":{"sound":"default"}}

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


你想播放特定的声音,但不知道如何指定吗?或者你正在努力获取任何声音吗? - Alex AIT
@AlexAIT 完全没有声音...之前还好好的... - shaqir saiyed
你是如何在后端构建通知的?能展示一些代码吗?任何带有 "sound": "default" 的警报都应该触发声音。 - Alex AIT
@AlexAIT 对的,但是要启用声音,我们需要在负载中包含“Sound”属性,但是由于Azure不支持“Sound”属性,API将变为错误请求,并且从Azure通知方面无法执行任何其他操作。他们要求联系APNS寻求任何替代方案(如果有的话)。他们还在论坛上开了这个问题:https://feedback.azure.com/forums/218849-notification-hubs/suggestions/38609911-add-support-for-critical-alerts-on-apns - shaqir saiyed
1个回答

3

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

您还可以使用通知服务应用扩展在通知即将发送之前添加声音文件。在您的扩展中,创建一个UNNotificationSound对象,并以与本地通知相同的方式将其添加到您的通知内容中。

为此需要创建一个新目标,您可能需要为通知扩展的bundle ID创建证书,使其与主应用程序相同。

进入图像描述

以下是供参考的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)
        }
    }
}

注意: 根据此文档,声音应该发送为default或任何自定义音频文件路径。没有提及忽略负载中的"Sound"键的行为,因为将其视为Sound是必须的!!。

编辑: 从这里的苹果文档可以看到,

在希望系统播放声音时,请包括此键。

如果找不到声音文件,或者为值指定了"default",则系统会播放默认警报声音。

这意味着如果负载中没有声音键,则不会播放声音。 它应该是default错误的音频路径


请问您能否分享任何官方文件或参考资料,其中提到了在有效载荷中没有“声音”参数的重要性或影响?我的所有应用程序都已上线,并且如果可能的话正在寻找一些快速解决方法。非常感谢。 - shaqir saiyed
1
@shaqirsaiyed,没有确切的文档说明声音是必须的还是可选的,这个文档说应该发送到default或任何自定义值不为空的声音。我建议启用通知扩展或通过AWS发送“声音”,最好提交一个新的构建。 - Gokul G
感谢您的支持,为了启用声音,我们需要在有效载荷中包含“Sound”属性,但是后端API将成为错误请求,因为Azure通知中心不支持“Sound”属性,除非他们更新他们的API,否则Azure通知中心无法执行其他操作。 - shaqir saiyed
1
我喜欢你的通知扩展方法和提交新版本的方式,但如果我无法应用任何快速解决方案或修复,我必须稍后考虑这个问题。谢谢。 - shaqir saiyed
1
@shaqirsaiyed,你有没有解决这个问题的办法?如果没有,你介意接受这个答案吗? - Gokul G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接