2013-06-11 5 views
5

in Alarm, Meldung arbeitet im Hintergrund fein wie folgt:Lokale Benachrichtigung in dem Vordergrund

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

    [email protected]"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); 

aber wenn ich ändere es im Vordergrund spielen auch wie folgt: .. seine nicht working..Only Warnung wird angezeigt, aber keine klingen???

-(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 

Wenn ich log Sound-Datei etc Eigenschaften notification..they funktionieren ... aber kein Ton gibt es ...

Antwort

8

Im Vordergrund Sie Warnungsansicht zur Verfügung stellen müssen und Sound spielen, wenn es erforderlich ist, Die Benachrichtigung wird nur applicationDidReceiveLocalNotification aufrufen. Sie können den Ton spielen 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]; 
7

Verwendung Wenn die Anwendung vorderste und sichtbar ist, wenn das System die Benachrichtigung liefert, wird kein Alarm angezeigt, wird kein Symbol badged, und kein Ton gespielt wird. Allerdings wird die Anwendung: didReceiveLocalNotification: aufgerufen, wenn der Anwendungsdelegat es implementiert. Die UILocalNotification-Instanz wird an diese Methode übergeben, und der Delegat kann seine Eigenschaften überprüfen oder auf benutzerdefinierte Daten aus dem userInfo-Wörterbuch zugreifen.