Why didrecivedlocalnotification method not working

2019-08-29 02:52发布

问题:

I have an application in which i have set a local notification in my class.m file which is show below:-

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

}

and i used code in my appdelegate file is that:-

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

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

}

Now error is that when notification is generate then didReceiveLocalNotification not call. How i fix this error?

Thanks in advances...

回答1:

  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification; function will call when we tap on view button of local notification not on close button.