Krunal Lathia
Krunal Lathia

Reputation: 97

local notification are sent at wrong time

I want to sent 2 local notifications daily at a particular time in iOS,

But notifications are sent at wrong time and also it sends multiple times in day and not once,

Here is my code snippet,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

 // are you running on iOS8?
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else // iOS 7 or earlier
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    application.applicationIconBadgeNumber = 0;



      //This if will be called only once....
     if ([prefs stringForKey:@"Notification"] == nil) 
     {


 //... 1st notification ...

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
    [components setHour:11];
    [components setMinute:24];

    // Gives us today's date but at 11am
    NSDate *next11am = [calendar dateFromComponents:components];
    if ([next11am timeIntervalSinceNow] < 0) {
        // If today's 9am already occurred, add 24hours to get to tomorrow's
        next11am = [next11am dateByAddingTimeInterval:60*60*24];
    }

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = next11am;
    notification.alertBody = @"Notification 1.";
    // Set a repeat interval to daily
    notification.repeatInterval = NSDayCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];




//... 2nd Notification ...

    NSDate *now1 = [NSDate date];
    NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components1 = [calendar1 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now1];
    [components1 setHour:18];
    [components1 setMinute:30];

    // Gives us today's date but at 9am
    NSDate *next6pm = [calendar dateFromComponents:components];
    if ([next6pm timeIntervalSinceNow] < 0) {
        // If today's 6pm already occurred, add 24hours to get to tomorrow's
        next6pm = [next6pm dateByAddingTimeInterval:60*60*24];
    }

    UILocalNotification *notification1 = [[UILocalNotification alloc] init];
    notification1.fireDate = next6pm;
    notification1.alertBody = @"Notification 2.";


    // Set a repeat interval to daily
    notification1.repeatInterval = NSDayCalendarUnit;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification1];


}
  }

Where i am doing mistake ? please help

Thanks in advance!!

Upvotes: 0

Views: 416

Answers (1)

Wain
Wain

Reputation: 119031

You check [prefs stringForKey:@"Notification"], but you haven't defined prefs so it could be nil, and you never appear to set the value for Notification so that could also be missing. Any of these things would mean you're adding when you don't expect to.

You're calling scheduleLocalNotification: but nowhere are you calling cancelLocalNotification: or cancelAllLocalNotifications so, depending on how the user uses the app, or how you're doing your testing, you can easily be adding multiple notifications at the same time. Use scheduledLocalNotifications to check what is currently scheduled.

You should probably also be using currentCalendar for the calendar as you need to respect the users settings.

Upvotes: 2

Related Questions