0

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

neena
  • 365
  • 1
  • 6
  • 22
  • 1
    You will probably want to schedule multiple notifications, one for each specific date that you want. – Paulw11 Jan 05 '17 at 11:01
  • Fix any date which came on Monday and then set the timeInterval to weekly. notification.repeatInterval = NSWeekCalendarUnit; – Sivajee Battina Jan 05 '17 at 11:02

2 Answers2

0

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;
Niharika
  • 1,188
  • 15
  • 37
0

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
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75