仅在周一发送本地通知

时间:2017-01-05 10:56:47

标签: ios swift localnotification

我想在每个星期一发送本地通知。假设我有一个场景,我必须在每个星期一发送药物提醒一个月。因此,一个月内将有4个通知。我的代码如下,但我无法弄清楚以下事项; 1)如何在特定日期发送通知 2)如何限制最大结束日期的通知。

发送通知的代码如下:

let notification = UILocalNotification()
                    notification.alertBody = "Take Medication"                       notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
                    notification.fireDate = NSDate()
                    notification.userInfo = ["title": "notification app", "UUID": "Some Unique Guid"]
UIApplication.sharedApplication().scheduleLocalNotification(notification) 

有人可以帮忙吗? 问候, NEENA

2 个答案:

答案 0 :(得分:0)

将NSWeekCalendarUnit添加到NSDateComponents并将repeatInterval设置为NSWeekCalendarUnit。 例如:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 2] ; // Monday
[componentsForFireDate setHour: 18] ; // 4PM 
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

  //...
  notification.repeatInterval = NSWeekCalendarUnit;

答案 1 :(得分:0)

您可以管理类似的内容,

   var notification = UILocalNotification()

    notification.fireDate! = fireDate // this should be monday with desired timr


    notification.repeatInterval = NSWeekCalendarUnit  //this will repeat every week