UILocalNotification没有在正确的时间被解雇

时间:2013-05-07 08:26:02

标签: ios objective-c uilocalnotification

我正在使用UILocalNotification,但它没有准时开火而是在指定时间后8-10分钟通知我。这是我正在使用的代码

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    NSDate *notificationDate = [dateFormat dateFromString:dateString];
    localNotif.fireDate = notificationDate;
    localNotif.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    // Notification details
    localNotif.alertBody = @"Appear";
    // Set the action button
    localNotif.alertAction = @"Action";

    localNotif.soundName = UILocalNotificationDefaultSoundName;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

我在CLLocationManager didEnterRegion 委托中使用此功能。我做错了什么?

1 个答案:

答案 0 :(得分:3)

这是工作代码:

UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 60];
n1.alertBody = @"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 90];
n2.alertBody = @"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];