gpt4 book ai didi

ios - 本地通知声音在前台不起作用

转载 作者:行者123 更新时间:2023-11-29 10:48:35 25 4
gpt4 key购买 nike

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody =[NSString stringWithFormat:@"meeting schedule from %@ ",[namelist objectAtIndex:i]];

localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = @"meetting.mp3";

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"timerInvoked" object:self];

我正在使用这些代码创建本地通知。本地通知声音在后台工作,但是当进入前台时,只有通知在没有声音的情况下触发

请帮我解决这个问题..提前致谢

最佳答案

来自documentation on UILocalNotification :

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.

因此,您无法在前台播放声音,但另一种选择是您可以使用 AVAudioPlayer 播放通知声音。

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],notification.soundName]];

AVAudioPlayer *newAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer = newAudioPlayer;
self.audioPlayer.numberOfLoops = -1;
[self.audioPlayer play];

关于ios - 本地通知声音在前台不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429288/

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