如何在应用程序处于前台状态时接收到推送通知时播放声音。
我的代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (application.applicationState == UIApplicationStateActive)
{
//create an audio player and play the sound
NSString *path = [[NSBundle mainBundle] pathForResource:@"whistle" ofType:@"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
theAudio.volume = 1.0;
[theAudio prepareToPlay];
[theAudio play];
}
NSLog(@"didReceiveRemoteNotification %@",userInfo);
}
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
可以触发震动。 - Laszlo