简体   繁体   English

每周特定日期的本地通知

[英]Local Notification every specific day of week

I need to create a local notification for a specific time (example at 18.00) fired every working day of week (not Saturday and Sunday) but I can't find examples or tutorial. 我需要在一周的每个工作日(不是星期六和星期日)发布特定时间(例如18.00)的本地通知,但我找不到示例或教程。 I want use with swift2.1. 我想用swift2.1。 How can I create this notification? 我该如何创建此通知? My problem is define the firedate correctly of the notification 我的问题是正确定义通知的被激活

You can use NSDateComponents to create the exact date for your local notification. 您可以使用NSDateComponents为本地通知创建确切的日期。 Here is the example, how do we create local notification with exact fire date: 以下是示例,我们如何使用确切的开火日期创建本地通知:

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate:now];//get the required calendar units

if (components.weekday>2) {
    components.weekOfYear+=1;//if already passed monday, make it next monday
}
components.weekday = 2;//Monday
components.hour = 11;
NSDate *fireDate = [calendar dateFromComponents:components];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.alertBody = @"alert message";
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

In this piece of code, I set the fire date to every Monday 11 am, and set it repeat weekly using repeatInterval of local notification. 在这段代码中,我将开火日期设置为每周一上午11点,并使用repeatInterval本地通知将其设置为每周重复一次。 Code maybe wrong, need to test, but I guess this gives you the basic idea, also I don't code in swift, so it's objective c. 代码可能不对,需要测试,但我想这给了你基本的想法,我也不用swift编码,所以这是客观的c。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM