gpt4 book ai didi

ios - 尝试创建每个工作日重复的 UILocalNotification

转载 作者:行者123 更新时间:2023-11-28 22:15:31 25 4
gpt4 key购买 nike

我正在尝试使用 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);
}

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

最佳答案

将这段代码添加到你的for循环中

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

}

关于ios - 尝试创建每个工作日重复的 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com