iOS 无法播放远程通知的自定义声音
iOS Unable to play custom sound for remote notifications
我一直在尝试为 iOS 上的通知实现自定义声音。
根据示例 3 here 中的 Apple 文档。我们需要做的就是确保推送通知负载应该是这样的:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
}
}
并且bingbong.aiff必须放在应用程序包中。
此外,如果应用程序处于活动状态,建议我添加以下代码:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"bingbong" ofType:@"aiff"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
"bingbong.aiff"
放在"SupportingFiles"
我仍然没有收到远程通知的自定义声音。
我已经完成了与此类似的其他 SO 问题,但这些与我正在做的没有什么不同。
How to play custom sound file when user get push notification?
Custom sounds in remote notifications iOS 10, swift 3
删除 didReceiveRemoteNotification
中的 AVPlayer
内容。
确保:
- 声音长度小于 25 秒
- 文件确实是支持的格式之一(aiff、wav、caf)
- 文件确实存在于您的包中(复制包资源)
您注册了推送通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
return true
}
名称与文件名完全匹配:
{
"aps" : {
"alert" : "message",
"badge" : 1,
"sound" : "name.aiff"
}
}
对于默认声音:
根据苹果文档和指南,要播放默认声音,您必须在声音文件中发送 "Default" 值。否则它也不会播放默认声音。
自定义声音:
您需要在 php script
** 中传递自定义声音文件名来代替 default
。
- 自定义声音文件必须存在于您的
Xcode
项目中
资源。
- 确保声音文件持续时间不超过 30 秒。除此以外
它将播放默认声音。
- 并且您必须添加以下键
UILocalNotificationDefaultSoundName
在 .plist
文件中。见下图:
Swift 5 答案,你可以用 AudioToolbox 做,非常适合短声音片段:
播放默认系统声音(这里是声音代码列表:http://iphonedevwiki.net/index.php/AudioServices)
导入音频工具箱
AudioServicesPlaySystemSound(1103)
播放您之前拖入 Xcode 项目的声音文件:
var soundID:SystemSoundID = 0
let filePath = Bundle.main.path(forResource: "file_name", ofType: "wav") // 或者 mp3,这取决于你的声音文件的扩展名
let soundURL = URL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(声音URL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)
我一直在尝试为 iOS 上的通知实现自定义声音。
根据示例 3 here 中的 Apple 文档。我们需要做的就是确保推送通知负载应该是这样的:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
}
}
并且bingbong.aiff必须放在应用程序包中。
此外,如果应用程序处于活动状态,建议我添加以下代码:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"bingbong" ofType:@"aiff"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}
"bingbong.aiff"
放在"SupportingFiles"
我仍然没有收到远程通知的自定义声音。
我已经完成了与此类似的其他 SO 问题,但这些与我正在做的没有什么不同。
How to play custom sound file when user get push notification?
Custom sounds in remote notifications iOS 10, swift 3
删除 didReceiveRemoteNotification
中的 AVPlayer
内容。
确保:
- 声音长度小于 25 秒
- 文件确实是支持的格式之一(aiff、wav、caf)
- 文件确实存在于您的包中(复制包资源)
您注册了推送通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() return true }
名称与文件名完全匹配:
{ "aps" : { "alert" : "message", "badge" : 1, "sound" : "name.aiff" } }
对于默认声音:
根据苹果文档和指南,要播放默认声音,您必须在声音文件中发送 "Default" 值。否则它也不会播放默认声音。
自定义声音:
您需要在 php script
** 中传递自定义声音文件名来代替 default
。
- 自定义声音文件必须存在于您的
Xcode
项目中 资源。 - 确保声音文件持续时间不超过 30 秒。除此以外 它将播放默认声音。
- 并且您必须添加以下键
UILocalNotificationDefaultSoundName
在.plist
文件中。见下图:
Swift 5 答案,你可以用 AudioToolbox 做,非常适合短声音片段:
播放默认系统声音(这里是声音代码列表:http://iphonedevwiki.net/index.php/AudioServices)
导入音频工具箱
AudioServicesPlaySystemSound(1103)
播放您之前拖入 Xcode 项目的声音文件:
var soundID:SystemSoundID = 0
let filePath = Bundle.main.path(forResource: "file_name", ofType: "wav") // 或者 mp3,这取决于你的声音文件的扩展名
let soundURL = URL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(声音URL as CFURL, &soundID)
AudioServicesPlaySystemSound(soundID)