How to play sound on receiving a push notification when the app is in the foreground state.
My Code :
- (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);
}