我在iPhone中使用UILocalNotification
来制作闹钟。单击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")
}
答案 0 :(得分:1)
这是因为你打电话
[app presentLocalNotificationNow:notification];
因此它会在您创建通知后立即显示通知。删除这行代码。
[app scheduleLocalNotification:notification]
;将工作得很好,并将在fireDate拍摄通知。
答案 1 :(得分:-1)
以下是UILocalnotification的一个例子:
-(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];
}
确保您设置的日期不是过去的!