ios - iOS推送通知声音停止使用天蓝色通知中心工作[根本没有声音]
问题描述
很久以来,我的 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
解决方案
有一个选项可以在本地向远程通知添加声音,这样就不需要依赖服务器来添加声音属性,
您还可以使用通知服务应用程序扩展在交付前不久将声音文件添加到通知中。在您的扩展程序中,创建一个 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)
}
}
}
注意:
根据此文档,声音应作为default
或任何custom sound file path
. 没有提及忽略有效载荷中的“声音”的行为,认为这Sound
是强制性的!!。
编辑:@shaqir 来自Apple 文档,
当您希望系统播放声音时,请包含此键。
如果找不到声音文件,或者您指定了默认值,系统会播放默认的警报声音。
表示如果有效载荷中没有声音键,则不会播放声音。应该是default
或wrong audio path
。
推荐阅读
- javascript - 快速会话在 mongo 和 node 中不起作用
- c++11 - 在 meyer 的单例实现中使用 new 线程安全吗?
- office-js - 桌面 Webviewer 进程终止时,Outlook Web 加载项不会加载
- python - 从输入类型文件中读取文本文件
- python - .txt 文件的 POST 请求
- reactjs - Chrome每次在同一会话中重新下载相同的图像
- python - 使用外键的属性作为模型中的字段
- node.js - 为什么我的控制台日志记录:`TimeoutOverflowWarning:4294967296000 不适合 32 位有符号整数`
- youtube-dl - youtube-dl 是否仍然有效(最新版本 youtube-dl-2020.2.16)?
- javascript - 读取流不执行触发/捕获错误