Question

I'm trying to create a repeating local notification using Xamarin.IOS. I want to have different alert body message each day, for example "It's monday", "It's tuesday" and so on.

The problem I have is that the only the first notification is working. After I've read the documentation and a few tutorials the correct way to create a repeating notification is to loop through each day and create a total of seven notifications that has the RepeatInterval property set to NSCalendarUnit.Weekday.

My code is a bit messy at the moment...

for (int i = 0; i <= 6; i++) {

                NSDateComponents components = gregCalendar.Components (NSCalendarUnit.Day | NSCalendarUnit.Year | NSCalendarUnit.Month, DateTime.Now.AddDays(i).ToNSDate());
                components.Day = DateTime.Now.AddDays(i).Day;
                components.Month = DateTime.Now.AddDays(i).Month;
                components.Year = DateTime.Now.AddDays(i).Year;
                NSDate referenceDate = gregCalendar.DateFromComponents (components);

                NSDateComponents componentsForFireDate = gregCalendar.Components (NSCalendarUnit.Year | NSCalendarUnit.Hour | NSCalendarUnit.Minute, referenceDate);
                componentsForFireDate.Year = components.Year;
                componentsForFireDate.Month = components.Month;
                componentsForFireDate.Day = components.Day;
                componentsForFireDate.Hour = 8;
                componentsForFireDate.Minute = 0;

                var dayOfWeek = (int)DateTime.Now.AddDays (i).DayOfWeek + 1;
                if (dayOfWeek == 8)
                    dayOfWeek = 1;

                componentsForFireDate.Weekday = dayOfWeek;

                NSDate fireDateOfNotification = gregCalendar.DateFromComponents (componentsForFireDate);

                UILocalNotification localNotification = new UILocalNotification ();
                localNotification.FireDate = fireDateOfNotification;
                localNotification.TimeZone = NSTimeZone.LocalTimeZone;
                localNotification.AlertBody = dayOfWeek;
                localNotification.AlertAction = "daily";
                localNotification.RepeatCalendar = NSCalendar.CurrentCalendar;
                localNotification.RepeatInterval = NSCalendarUnit.Weekday;
                localNotification.ApplicationIconBadgeNumber = 1;

                UIApplication.SharedApplication.ScheduleLocalNotification (localNotification);
            }

My question is, how can I create a local notification that should repeat every day but with different alert body message depending on the weekday?

Was it helpful?

Solution

Add this code into you for loop

switch(i)
{
  case 0: //mon
           localNotification.alertbody = @"Monday....you message";
   break;
  case 1 //tue 
           localNotification.alertbody = @"Tuesday....you message";
   break;
  case 2://wed
          localNotification.alertbody = @"Wednesday....you message";
   break;
  ...
  ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top