Iphone Appdelegate函数';未收到本地通知';在创建通知后立即调用

Iphone Appdelegate函数';未收到本地通知';在创建通知后立即调用,iphone,objective-c,xcode4.3,uilocalnotification,appdelegate,Iphone,Objective C,Xcode4.3,Uilocalnotification,Appdelegate,我正在使用iPhone中的UILocalNotifications创建警报。当我单击ui按钮时,会安排报警。我的问题是,当我单击此按钮时,即当我创建本地通知时,调用了–application:didReceiveLocalNotification:。但是,–application:didReceiveLocalNotification:只应在到达通知时间时调用。我在模拟器和设备上都进行了检查,得到了相同的结果。谁能帮我做这个…提前谢谢 -(void)createalarm:(NSString *

我正在使用iPhone中的
UILocalNotification
s创建警报。当我单击
ui按钮时,会安排报警。我的问题是,当我单击此按钮时,即当我创建本地通知时,调用了
–application:didReceiveLocalNotification:
。但是,
–application:didReceiveLocalNotification:
只应在到达通知时间时调用。我在模拟器和设备上都进行了检查,得到了相同的结果。谁能帮我做这个…提前谢谢

-(void)createalarm:(NSString *)datend_time:(NSString *)message//creating alarm
{
NSLog(@"created!!!!!!!!!!!!!");
NSString *datestr = datend_time;
NSString *textstr = message;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];    
NSDate *alerttime = [formatter dateFromString:datestr];
UIApplication * app = [UIApplication sharedApplication];    

UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
    notification.fireDate = alerttime;
    //notification.timeZone = [NSTimeZone defaultTimeZone];
    notification.repeatInterval = 0;
    notification.alertBody = textstr;
    notification.soundName = UILocalNotificationDefaultSoundName;        
    [app scheduleLocalNotification:notification];

    [app presentLocalNotificationNow:notification];          
}

}


-(void)application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification
{
NSLOG(@"delegate called")    
}

以下是NSLocalnotification的一个示例:

-(void) scheduleNotificationForDate: (NSDate*)date {

/* Here we cancel all previously scheduled notifications */
 [[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

localNotification.fireDate = date;
NSLog(@"Notification will be shown on: %@",localNotification.fireDate);

localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:
                              @"Your notification message"];
localNotification.alertAction = NSLocalizedString(@"View details", nil);

/* Here we set notification sound and badge on the app's icon "-1" 
means that number indicator on the badge will be decreased by one 
- so there will be no badge on the icon */

localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

确保您设置的日期不是过去

那是因为你打电话

[app presentLocalNotificationNow:notification];  
因此,它会在创建通知后立即显示通知。删除这行代码


[appscheduleLocalNotification:notification]
;工作正常,会在fireDate发布通知。

我已经检查过了。一、 我确信我设定的日期不是过去。