2012-02-12 110 views
3

尝试使用下面的代码来安排UILocalNotification时碰到的问题:UILocalNotification - 通知不显示

- (IBAction) createNotification { 
    //Just to verify button called the method 
    NSLog(@"createNotification"); 

    NSString *dateString = dateTextField.text; 

    NSString *textString = textTextField.text; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MM-dd-yyyy HH:mm"]; 
    [formatter setTimeZone: [NSTimeZone defaultTimeZone]]; 

    NSDate *alertTime = [formatter dateFromString:dateString]; 

    UIApplication *app = [UIApplication sharedApplication]; 

    UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 
    if(notification){ 
     notification.fireDate = alertTime; 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 
     notification.repeatInterval = 0; 
     notification.alertBody = textString; 

     [app scheduleLocalNotification:notification]; 

     NSLog(@"%@", dateString); 
    } 
} 

代码的调用是否正确,当我按下按钮,我传递以下日期字符串:

2012年2月12日19:01

  • 我也试过

02/12/2012 19:01

无济于事。 (相应地根据测试时间改变时间,例如21:06)

有人可以解释为什么本地通知不显示吗?

由于提前,

杰克

+0

当你执行NSLog(@“%@”,alertTime)'时会发生什么? – yuji 2012-02-12 20:13:47

+0

你可以尝试以下代码来使用presentLocalNotificationNow替换scheduleLocalNotification以确保本地通知正常工作吗?你正在使用哪个iOS版本? – Allen 2012-02-12 20:14:35

+0

您是否尝试过NSLog notification.fireDate以查看它显示的时间? – Shubhank 2012-02-12 20:16:49

回答

10

本地通知交付,但不显示(即无证件,无声音,无警示),当应用程序正在运行,并在前台。但是,如果您需要以某种方式对本地通知作出反应,则会调用您的应用程序委托的application:didReceiveLocalNotification:方法。

有关详细信息,请参阅UILocalNotification class reference

+0

谢谢,但我的应用程序不调用didReceiveLocalNotification?任何想法,为什么这可能是?谢谢 – 2012-02-12 22:00:08

+0

所以你说你可以安排通知,如果应用程序没有运行,你会看到通知,但是如果你按照同样的方式安排通知并且应用程序正在运行,那么'application:didReceiveLocalNotification'没有被调用? – yuji 2012-02-12 22:03:27

+0

是的,这就是它。我使用这个: - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {NSLog(@“Notification Received”); } – 2012-02-12 22:08:08