UILocalNotification即刻开火

时间:2016-06-21 09:10:12

标签: ios objective-c xcode uilocalnotification

我已经过了今天的日期和当前时间之前的时间。当代码执行时,它仍然会立即触发。

它在日志中提供正确的输出。

localNotification <UIConcreteLocalNotification: 0x7a8cc990>{
fire date = Tuesday, 21 June 2016 at 12:30:00 PM GMT, time zone = GMT (GMT) offset 0, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, 21 June 2016 at 12:30:00 PM Eastern Daylight Time, user info = {
    "FIRE_TIME_KEY" = "2016-06-21 12:30:00 +0000";
    NotifySound = Default;
    "Program_Id" = 1114;
    "Program_Name" = "Nutrition For Ch1";
    notification = Start;
}}

这是我的代码:

NSDate *date = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy-MM-dd"];
NSString *strDt = [format stringFromDate:date];

NSString *strDate = [NSString stringWithFormat:@"%@ %@",strDt,strStartSelectedDateTime];
date = [formater dateFromString:strDate];
NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:date,@"FIRE_TIME_KEY",
                                          [[remainingScheArr valueForKey:kUser_Program_Mst_Program_Id] objectAtIndex:h],kUser_Program_Mst_Program_Id,
                                          [NSString stringWithFormat:@"%@ For %@",[[remainingScheArr valueForKey:kProgram_Mst_Program_Name] objectAtIndex:h],
                                           [[NSUserDefaults standardUserDefaults]valueForKey:kSelected_Student]],kProgram_Mst_Program_Name,
                                          @"Start",@"notification",
                                          strNotifySoundPath,@"NotifySound",
                                          nil];
[self performSelector:@selector(scheduleNotificationWithItem:andRepeatInterval:) withObject:dataDict withObject:nil];


- (void)scheduleNotificationWithItem:(NSDictionary*)item  andRepeatInterval:(NSCalendarUnit)CalUnit
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate =  [item valueForKey:@"FIRE_TIME_KEY"];

    localNotification.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;


    localNotification.alertBody = [NSString stringWithFormat:@"Remember: %@ starts at %@.",value,value];

    localNotification.alertAction = NSLocalizedString(@"View Details", nil);

    localNotification.userInfo = item;

    localNotification.repeatInterval = CalUnit;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

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

没有任何错误。它正在成功触发,但不是在预定的时间,只是执行它正在触发的代码。

1 个答案:

答案 0 :(得分:0)

您正在尝试安排本地通知已过去。

NSDate *date = [NSDate date]; // Will give you current date time and Notification will fire immediately.

您需要在日期中添加一些时间间隔。

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60]; //Will fire after 60 seconds.

希望这会有所帮助。

Rgds,

阿米特