ios - 在 UNNotificationServiceExtension 中为 iOS 远程通知添加系统声音

标签 ios swift xcode remote-notifications

我正在使用 UINotificationServiceExtension 成功修改我的远程通知负载。

我想根据用户在这里的选择更改提示音。为此,我需要将 UINotificationSound 对象分配给 bestAttemptContent:

bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "alertTone.caf"))

这适用于我创建并导入到我的项目中的音调。上例中的一个名为 alertTone.caf。

但是,我想使用系统提示音而不是我自己的提示音。我知道我可以设置默认音调,但我想访问其他自定义音调。有没有办法做到这一点?

最佳答案

UNNotificationSound 不能引用系统声音,但可以尝试在服务扩展中收到通知时播放声音。

// import
import AVFoundation

// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016

// Play the correct sound when the notification is received in the service extension
AudioServicesPlaySystemSound (systemSoundID)

播放系统声音的来源here .

This是您可以用作引用的所有系统声音 ID 的列表。

关于ios - 在 UNNotificationServiceExtension 中为 iOS 远程通知添加系统声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913791/

相关文章:

ios - 如何使用连接 USB 线调试在移动设备中执行的 iOS 应用程序(不是由 Xcode 启动)

ios - 如何将 MessagesViewController 扩展到全屏?

xcode - 使用运行脚本比较来自 info.plist 的值

ios - API JSON 和 UIPageViewController 可选 Nil

ios - 在 App Store 上更新 Only iPad 应用程序

objective-c - 在身份检查器中找不到 Storyboard ID

ios - iPad3 - 将相机拍摄的照片保存到应用程序的文档文件夹中

ios - 无限登录循环?

ios - 只能通过覆盖 heightForRowAtIndexPath 来更改静态 UITableViewCell 的高度?

ios - UIViewController Storyboard 自定义尺寸