尝试在每个工作日重复创建UILocalNotification

时间:2014-02-17 05:16:43

标签: ios xamarin.ios xamarin uilocalnotification

我正在尝试使用Xamarin.IOS创建重复的本地通知。我希望每天都有不同的警报正文消息,例如“星期一”,“星期二”等等。

我遇到的问题是只有第一个通知正常。在我阅读了文档和一些教程之后,创建重复通知的正确方法是遍历每一天并创建总共七个通知,其中RepeatInterval属性设置为NSCalendarUnit.Weekday。

我的代码目前有点乱......

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);
            }

我的问题是,我如何创建一个本地通知,该通知应该每天重复,但根据工作日不同的警报正文消息?

1 个答案:

答案 0 :(得分:2)

将此代码添加到循环

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;
  ...
  ...

}