Hassy
Hassy

Reputation: 5228

UILocalNotification not being fired on right time

I am using UILocalNotification but it is not firing on time rather it notifies me 8-10 min after the specified time. Here is the code i am using

    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];

I am using this in didEnterRegion delegate of CLLocationManager. What i am doing wrong?

Upvotes: 1

Views: 466

Answers (1)

Priyank Gandhi
Priyank Gandhi

Reputation: 1253

This is Working Code:

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];

Upvotes: 3

Related Questions