【发布时间】:2016-11-08 11:26:37
【问题描述】:
我一直在尝试为 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 问题,但这些问题与我正在做的事情没有什么不同。
【问题讨论】:
-
向你的脚本展示你是如何从服务器触发通知的。
-
应用打开时调用
didReceiveRemoteNotification函数并收到推送,打开应用时是否听到声音? -
@iphonic 不,应用打开时我听不到任何声音。在后台播放默认声音。
-
@Ichthyocentaurs 尝试在
didReceiveRemoteNotification中设置断点,看看是否获得了正确的文件,可能是您的声音文件有问题?
标签: ios objective-c apple-push-notifications