在闹钟中,通知在后台正常工作如下:
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
当我记录声音文件等通知属性时......它们工作正常......但是没有声音......
答案 0 :(得分: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];
答案 1 :(得分:7)
如果应用程序是最重要的,并且在系统交付时可见 通知,没有显示警报,没有图标标记,没有声音 玩过的。但是,应用程序:didReceiveLocalNotification:是 如果应用程序委托实现它,则调用它。该 UILocalNotification实例传递给这个方法,并且 委托可以检查其属性或访问来自的任何自定义数据 userInfo字典。