Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local notification in Foreground

in alarm ,notification works fine in background as follows:

    UILocalNotification *notification1=[[UILocalNotification alloc]init];
    notification1.fireDate=alramtime;
    notification1.alertBody=@"Training Time";
    notification1.repeatInterval=NSDayCalendarUnit;

    notification1.soundName=@"Alarm.caf";

    ///////
    previousnotif=[[NSUserDefaults standardUserDefaults]objectForKey:@"notif1"];
    previous=[NSKeyedUnarchiver unarchiveObjectWithData:previousnotif];

    NSLog(@"alarm %@",previous);
    if (previous!= NULL) {
        [[UIApplication sharedApplication]cancelLocalNotification:previous];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"notif1"];

    }
    NSData *alarm1=[NSKeyedArchiver archivedDataWithRootObject:notification1];
    [notifdefaults setObject:alarm1 forKey:@"notif1"];
    /////////


    [[UIApplication sharedApplication] scheduleLocalNotification:notification1];
    NSLog(@"new alarm %@",notification1);

but when i modify it to play in foreground too as follows:..its not working..Only alert appears but no sound???

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {


   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"KNIP"
                                                   message:notification.alertBody
                                                   delegate:self cancelButtonTitle:@"Close"
                                          otherButtonTitles:nil];  

[alert show];

}
@end

When i log soundfile etc properties of notification..they work fine...but no sound is there...

like image 469
user2474504 Avatar asked Jun 11 '13 12:06

user2474504


1 Answers

In foreground you have to provide alert view and play sound if it requires, the notification will just call applicationDidReceiveLocalNotification. You can play the sound using AVAudioPlayer

 //Playing sound
        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];
        [newAudioPlayer release];
like image 125
Anil Varghese Avatar answered Oct 12 '22 23:10

Anil Varghese