2013-06-11 5 views
5

でローカル通知、次のように通知がバックグラウンドで正常に動作します:アラームの前景

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

が、私は次のようにあまりにもフォアグラウンドで再生するためにそれを変更..そのworking..Onlyない警告が表示されますがありません音???

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

私はログ サウンドファイルnotification..they仕事の性質など細かい...しかし、音がしない...あなたは、アラートビューを提供し、それが必要な場合は、サウンドを再生する必要がフォアグラウンドで

答えて

8

、通知は単にapplicationDidReceiveLocalNotificationを呼び出します。あなたは、システムが 通知を配信するとき、アプリケーションが一番と表示されている場合は、何の警告が示されていない、アイコンがバッジず、何の音 が再生されていない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

を使用してサウンドを再生することができます。ただし、アプリケーション:didReceiveLocalNotification:は、アプリケーション代理人が実装する場合は です。 UILocalNotificationインスタンスがこのメソッドに渡され、 デリゲートは、そのプロパティを確認したり、 userInfo辞書からカスタムデータにアクセスできます。

関連する問題