2014-02-17 70 views
0

我試圖使用Xamarin.IOS創建重複本地通知。我希望每天都有不同的警報主體信息,例如「它是星期一」,「它是星期二」等等。試圖創建每個工作日重複的UILocalNotification

我遇到的問題是唯一的第一個通知正在工作。在閱讀完文檔和一些教程之後,創建重複通知的正確方法是每天循環並創建總計七個將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); 
      } 

我的問題是,我怎麼可以創建應該每天但取決於平日不同的警報體消息重複本地通知?

+0

對於您可以切換我6案值,併爲每個通知設置不同的警報 –

回答

1

這段代碼添加到您的循環

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

} 
相關問題