为什么我的UILocalNotification没有播放任何声音?

nbu*_*urk 4 audio objective-c ios uilocalnotification

这让我疯了,我相信我现在已经尝试了所有可能的方法,但我的手机仍然决定保持沉默.当我提出本地通知时,我只想让它播放任何声音.目前,UILocalNotificationDefaultSoundName我添加到我的应用包中的自定义声音和自定义声音都不起作用.我写了一个简单的倒计时应用程序,presentLocalNotification:在倒计时结束时使用.

- (void)showLocalNotification
{
   UILocalNotification *alarmNotification = [[UILocalNotification alloc] init];
   alarmNotification.alertBody = [NSString stringWithFormat:@"fired at: %@", [NSDate date]];
   alarmNotification.soundName = UILocalNotificationDefaultSoundName; 
   // alarmNotification.soundName = @"glass.aiff"; // "glass.aiff" is a file in my application bundle
   [[UIApplication sharedApplication] presentLocalNotificationNow:alarmNotification];
}
Run Code Online (Sandbox Code Playgroud)

我还会显示一个UIAlertView将从AppDelegate收到通知时初始化并显示的内容:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"  message:@"Local notification was received" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alertView show];
}
Run Code Online (Sandbox Code Playgroud)

收到本地通知并显示警报,在通知中心我也可以看到通知已被触发,只有声音不起作用...

我检查了堆栈溢出中的每个类似问题,启用了我的所有声音设置,确保在启用应用程序的电话通知设置时,我甚至在我测试声音播放的位置时收到了来自其他应用程序的推送通知. ..

我到底错过了我的手机什么都没发出声音?当我触发通知或收到通知时,是否必须导入一些框架或调用其他任何内容?

也许最简单的解决方案是,如果有人在发布通知时发布播放默认声音所需的代码的最小版本(尽管我相信我自己已经编写了最小版本 - 除了它不起作用).

顺便说一句,我在真实设备和模拟器上都尝试了这个应用程序,其中没有一个能够工作......

通知中心证明我的所有本地通知都被解雇了 警报证明我的应用代表收到本地通知

小智 9

如果您的应用程序在通知触发时位于前台,则不会自动播放声音.如果您的应用当时处于后台,它将仅自动播放声音.

您的屏幕截图看起来像您的应用程序可能在您尝试时在前台处于活动状态.

根据Apple的推送通知文档:

如果应用程序在系统发送通知时最重要且可见,则不会显示警报,也不会标记图标,也不会播放任何声音.但是,如果应用程序委托实现它,则调用application:didReceiveLocalNotification :. UILocalNotification实例将传递到此方法,并且委托可以检查其属性或从userInfo字典访问任何自定义数据.

如果您愿意,您可以使用AudioServicesPlaySystemSound()方法自己播放声音.