【问题标题】:Why didrecivedlocalnotification method not working?为什么 didrecivedlocalnotification 方法不起作用?
【发布时间】:2012-01-14 16:31:02
【问题描述】:

我有一个应用程序,我在我的 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 {

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

}

现在的错误是当通知生成时 didReceiveLocalNotification 没有调用。我如何解决这个错误?

提前致谢...

【问题讨论】:

  • 此方法在应用程序处于前台时有效
  • @iPhone Developer 我如何改进?
  • 你是说通知发生了但是这个方法没有被调用,当你的应用在后台时你不能调用任何方法。
  • @iPhone 开发者你知道通知发布时调用方法的方法吗?
  • 当且仅当应用程序在前台时,它才会在您的通知发生时自动被调用。

标签: iphone uilocalnotification uiapplicationdelegate


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

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2013-01-22
    • 2018-08-06
    • 2015-11-07
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多