Local notifications only on Monday

512 views Asked by At

I am trying to send a local notifications on every Monday. Let say that I have a scenario in which I have to send a medication taking reminder on every Monday for one month. So it will be total 4 notification in a month. My code is as below but I can't figure out the following things; 1)How to send the notifications on a specific day 2)How to limit the notification for a max end date.

The code for sending the notification is as follows;

let notification = UILocalNotification()
                    notification.alertBody = "Take Medication"                       notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
                    notification.fireDate = NSDate()
                    notification.userInfo = ["title": "notification app", "UUID": "Some Unique Guid"]
UIApplication.sharedApplication().scheduleLocalNotification(notification) 

Can anyone please help? Regards, neena

2

There are 2 answers

0
Niharika On

Add the NSWeekCalendarUnit to NSDateComponents and set the repeatInterval to NSWeekCalendarUnit. For Example :

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 2] ; // Monday
[componentsForFireDate setHour: 18] ; // 4PM 
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

  //...
  notification.repeatInterval = NSWeekCalendarUnit;
2
Ketan Parmar On

You can manage something like,

   var notification = UILocalNotification()

    notification.fireDate! = fireDate // this should be monday with desired timr


    notification.repeatInterval = NSWeekCalendarUnit  //this will repeat every week