为什么恢复的本地通知方法不起作用?

时间:2011-12-07 05:04:01

标签: iphone uilocalnotification uiapplicationdelegate

我有一个应用程序,我在class.m文件中设置了一个本地通知,如下所示: -

-(IBAction)save{
NSString *str1=[NSString stringWithFormat:@"%@",txt_date.text];
NSString *str2=[NSString stringWithFormat:@" %@",txt_time.text];
str1=[str1 stringByAppendingFormat:str2];
selected_label.text= str1;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *today=[NSDate date]; 
NSDateFormatter* formatter_current = [[[NSDateFormatter alloc] init] autorelease];
formatter_current.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
//Set the required date format 
[formatter_current setDateFormat:@"yyyy-MM-dd hh:mm a"]; 

NSLog(@"current date is =%@",str1); 
today=[formatter_current dateFromString:str1];
NSLog(@"current date:-%@",today); 
UILocalNotification* ln = [[UILocalNotification alloc] init];
//ln.alertBody = @"Wake Up Sid";
//ln.applicationIconBadgeNumber = 1;
ln.fireDate = today; //[NSDate dateWithTimeIntervalSinceNow:15];
ln.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSLog(@"alarm will activate on%@",today);
NSDateFormatter* formatter_alarm = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter_alarm setLocale:uslocale];
[uslocale release]; 
formatter_alarm.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter_alarm setDateFormat:@"hh:mm a"]; 
NSString *str=[formatter_alarm stringFromDate:today];
NSLog(@"%@",str);
//ln.alertBody = [NSString stringWithFormat:@"Your first appointment at %@",str];
//ln.soundName = UILocalNotificationDefaultSoundName;
ln.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
[ln release];

}

我在我的appdelegate文件中使用的代码是: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// Override point for customization after application launch.


self.viewController=[[demo_social_updatesViewController alloc]initWithNibName:@"demo_social_updatesViewController" bundle:nil];
nav_controller=[[UINavigationController alloc] initWithRootViewController:self.viewController];
// Add the view controller's view to the window and display.
[self.window addSubview:nav_controller.view];
[self.window makeKeyAndVisible];
appDelegate_acess_token=[[NSUserDefaults standardUserDefaults] stringForKey:@"access_token"];
  application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
    NSLog(@"Recieved Notification %@",localNotif);
}
return YES;}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {/ / p>

     NSLog(@"Recieved Notification %@",notification);

}

现在错误是当生成通知时,didReceiveLocalNotification不会调用。我如何修复此错误?

谢谢你的进步......

1 个答案:

答案 0 :(得分:2)

  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;当我们点击本地通知的查看按钮而不是关闭按钮时,函数将会调用。