gpt4 book ai didi

ios - 为什么我的 UILocalNotification 不播放任何声音?

转载 作者:可可西里 更新时间:2023-11-01 05:02:45 26 4
gpt4 key购买 nike

这让我抓狂,我相信我现在已经尝试了所有可能的方法,但我的手机仍然决定保持静音。我只想让它在我显示本地通知时播放 任何 声音。目前,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];
}

我还展示了一个 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];
}

收到本地通知并显示警报,在通知中心我也可以看到通知已触发,只是声音没有响...

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

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

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

顺便说一下,我在真实设备和模拟器中都尝试了该应用程序,但都不起作用...

the notification center proves that all my local notifications are fired the alert proves that my app delegate receives the local notification

最佳答案

如果通知触发时您的应用程序在前台,则不会自动播放声音。如果您的应用当时在后台,它只会自动播放声音。

您的屏幕截图看起来像是您的应用程序在您尝试时可能在前台处于事件状态。

根据 Apple 的推送通知文档:

If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

如果需要,您始终可以使用 AudioServicesPlaySystemSound() 方法自己播放声音。

关于ios - 为什么我的 UILocalNotification 不播放任何声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23532706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com