1

I am working on app which having functionality to show local notification.

In that, I set local notification for specific date after that I want to show it for next consecutively 84 days (12 weeks).For this I am using NSDayCalendarUnit repeat type. But those reminders are still showing after 84 days.

I tried - (void)applicationDidBecomeActive:(UIApplication *)application method to cancel notification but It needs at least one interaction with app after 84 days to Know the the system date.

Please give some solution or guideline on it. Following code I am using to set reminders.

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];


// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:cDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:cDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];

Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {

    UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [calendar dateFromComponents:dateComps];;
    notif.timeZone = [NSTimeZone defaultTimeZone];

    notif.alertBody =@"take your medicine";
    notif.alertAction = @"View";


    NSLog(@"SOUND=%d",[[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]);

    if([[NSUserDefaults standardUserDefaults] boolForKey:SWITCH_SOUND]||cId>=100)
    notif.soundName = UILocalNotificationDefaultSoundName;

    notif.repeatInterval=NSCalendarUnitDay;
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",cId]
                                                         forKey:NOTIFICATION_KEY];
    notif.userInfo = userDict;


    [[UIApplication sharedApplication] scheduleLocalNotification:notif];

    NSLog(@"Set reminder %d For Date =%@",cId,cDate);
}
Vishal's p
  • 198
  • 4
  • 19

0 Answers0