1

UILocalNotification在 iPhone 中使用 s 来创建警报。当我单击 a 时,会安排警报UIButton。我的问题是– 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")    
}
4

2 回答 2

1

这是因为你打电话

[app presentLocalNotificationNow:notification];  

因此,它会在您创建通知后立即显示。删除这行代码。

[app scheduleLocalNotification:notification]; 可以正常工作,并会在 fireDate 发出通知。

于 2013-10-19T07:33:30.890 回答
-1

下面是一个 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];
}

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

于 2012-09-04T07:35:31.033 回答