【问题标题】:iOS push notification sound stopped working using azure notification hub [ no sound at all ]iOS推送通知声音停止使用天蓝色通知中心工作[根本没有声音]
【发布时间】:2021-09-21 14:48:09
【问题描述】:

很久以来,我的 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":{"sound":"default"}}

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

【问题讨论】:

  • 您想播放特定的声音,但不知道如何指定?或者您正在为获得任何声音而苦苦挣扎?
  • @AlexAIT 完全没有声音......之前它工作正常......
  • 您如何在后端构建通知?你能显示一些代码吗?任何带有"sound": "default" 的警报都会触发声音。
  • @AlexAIT 对,但问题是要启用声音,我们需要在有效负载中包含“声音”属性,但 API 会成为错误请求,因为 azure 不支持“声音”属性和天蓝色通知端无法执行任何其他操作。他们要求联系 APNS 以寻求任何替代方案(如果有的话)。他们的论坛上也有这张公开票:feedback.azure.com/forums/218849-notification-hubs/suggestions/…

标签: ios azure audio push-notification


【解决方案1】:

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

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

为此需要创建一个新目标,您可能需要为通知扩展的捆绑 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 doc 声音应以default 或任何custom sound file path 发送。没有提及忽略有效载荷中的“声音”的行为,因为Sound 是强制性的!!。

编辑: @shaqir 来自Apple documentation here

当您希望系统播放声音时,请包含此键。

如果声音文件找不到,或者你指定了default的值,系统会播放默认的提示音。

表示如果有效载荷中没有声音键,则不会播放声音。应该是defaultwrong audio path

【讨论】:

  • 您能否分享任何官方文档或参考资料,其中他们提到了在有效载荷中没有“声音”参数的重要性或影响?我所有的应用程序都在商店里,如果可能的话,正在寻找一些快速修复。非常感谢
  • @shaqirsaiyed,没有确切的文档说声音是强制性的或可选的,This 文档说声音应该发送到 default 或任何不为空的自定义值。我最好建议提交一个启用通知扩展的新版本,或者从 AWS 发送“声音”
  • 谢谢,要启用声音,我们需要在有效负载中包含“声音”属性,但后端的 API 会变成错误请求,因为 azure 通知中心不支持“声音”属性,并且有除了更新其 API 之外,无法从 azure 通知端执行任何其他操作。
  • 我喜欢您的通知扩展方法并提交了一个新版本,但如果我无法快速应用或修复,我必须稍后考虑这一点。谢谢
  • @shaqirsaiyed 你有什么办法解决这个问题吗?如果不是,你介意接受这个答案
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
相关资源
最近更新 更多