2013-06-11 29 views
5

trong báo động, thông báo hoạt động tốt ở chế độ nền như sau:thông báo địa phương tại Foreground

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

nhưng khi tôi thay đổi nó để chơi trong foreground quá như sau: .. không working..Only cảnh báo của nó xuất hiện nhưng không có âm thanh???

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

Khi tôi đăng nhập soundfile vv tính chất của công việc notification..they tốt ... nhưng không có âm thanh là có ...

Trả lời

8

Trong foreground bạn phải cung cấp cái nhìn tỉnh táo và chơi âm thanh nếu nó đòi hỏi, thông báo sẽ chỉ gọi applicationDidReceiveLocalNotification. Bạn có thể chơi âm thanh sử dụng 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

Nếu ứng dụng là quan trọng nhất và có thể nhìn thấy khi hệ thống cung cấp thông báo, không có cảnh báo được hiển thị, không có biểu tượng được badged, và không có âm thanh được chơi. Tuy nhiên, ứng dụng: didReceiveLocalNotification: là được gọi nếu ứng dụng ủy nhiệm thực hiện nó. Ví dụ UILocalNotification được chuyển vào phương thức này và đại biểu có thể kiểm tra thuộc tính của nó hoặc truy cập bất kỳ dữ liệu tùy chỉnh nào từ từ điển userInfo .

Các vấn đề liên quan