【发布时间】:2014-02-17 05:16:42
【问题描述】:
我正在尝试使用 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);
}
我的问题是,如何创建一个本地通知,该通知应该每天重复,但根据工作日的不同而发出不同的警报正文?
【问题讨论】:
-
为此您可以为 6 种情况切换 i 值并为每个通知设置不同的警报
标签: ios xamarin.ios xamarin uilocalnotification