本地通知声音在前景中不起作用

时间:2014-01-29 11:09:58

标签: ios iphone objective-c

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];

我在create local notification使用这些代码。本地通知声音在后台but when Enters to foreground only notification fire without sound中有效。

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

1 个答案:

答案 0 :(得分:2)

来自documentation on UILocalNotification

  

如果应用程序是最重要的,并且在系统交付时可见   通知,没有显示警报,没有图标标记,没有声音   玩过的。但是,应用程序:didReceiveLocalNotification:是   如果应用程序委托实现它,则调用它。该   UILocalNotification实例传递给这个方法,并且   委托可以检查其属性或访问来自的任何自定义数据   userInfo字典。

因此,您无法在前景中播放声音,但另一个选项是您可以使用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];
相关问题