Iphone UILocalNotification-通知未显示

Iphone UILocalNotification-通知未显示,iphone,objective-c,uilocalnotification,Iphone,Objective C,Uilocalnotification,尝试使用以下代码计划UILocalNotification时遇到问题: - (IBAction) createNotification { //Just to verify button called the method NSLog(@"createNotification"); NSString *dateString = dateTextField.text; NSString *textString = textTextField.text;

尝试使用以下代码计划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);
    }
}
当我按下按钮并输入以下日期字符串时,代码被正确调用:

02-12-2012 19:01

  • 我也试过了
2012年12月2日19:01

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

有人能解释一下为什么本地通知没有显示吗

提前感谢,


Jack

当应用程序正在运行且在前台时,会发送本地通知,但不会显示(即无徽章、无声音、无警报)。但是,如果您需要以某种方式响应本地通知,则会调用应用程序委托的
application:didReceiveLocalNotification:
方法


有关详细信息,请参阅。

执行NSLog(@“%@”,alertTime)时会发生什么情况?是否可以尝试以下操作,用presentLocalNotificationNow替换scheduleLocalNotification,以确保本地通知正常工作?您正在使用哪个iOS版本?是否尝试记录notification.fireDate以查看它显示的时间?使用输入02-12-2012 20:35时,NSLog(@“%@”,notification.fireDate)返回2012-02-12 20:35:00+0000。虽然它现在可以工作了,但如果应用程序处于活动状态,它不会启动,我正在使用通知执行一段代码来存储和重置某些变量中的数据,如果应用程序处于活动状态,通知中的代码还会启动吗?还是应该使用其他方法?谢谢,但我的应用程序没有调用didReceiveLocalNotification?知道为什么会这样吗?谢谢你是说你可以安排通知,如果应用程序没有运行,你会看到通知,但是如果你以同样的方式安排通知并且应用程序正在运行,那么
application:didReceiveLocalNotification
不会被调用?是的,就是这样。我使用的是:-(void)application:(UIApplication*)app didReceiveLocalNotification:(UILocalNotification*)notif{NSLog(@“Notification Received”);}恐怕我不知道——我只是在我这方面尝试了类似的方法,效果不错。如果应用程序未激活时显示通知,则应在应用程序激活时调用
application:didReceiveLocalNotification:
。你在应用程序代理中实现了这一点,对吗?