在前台的本地通知
在报警中,通知在后台正常工作如下:
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);
但是当我修改它在前台也玩如下:..它不工作..只有警报出现,但没有声音?
-(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
当我logging声音文件等通知属性..他们工作正常…但没有声音…
在前台你必须提供警报视图和播放声音,如果需要的话,通知将只是调用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];
如果应用程序是最重要的,并且在系统发送通知时可见,则不显示警报,不显示图标,也不播放声音。 但是,应用程序:didReceiveLocalNotification:如果应用程序委托实现它,则会被调用。 UILocalNotification实例被传递给这个方法,委托可以检查它的属性,或者访问userInfo字典中的任何自定义数据。