繁体   English   中英

仅在星期一进行本地通知

[英]Local notifications only on Monday

我正尝试在每个星期一发送本地通知。 假设我有一种情况,我必须在每个星期一发送一个服药提醒,持续一个月。 因此,一个月内总共会有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) 

谁能帮忙吗? 问候,娜娜

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

您可以管理类似

   var notification = UILocalNotification()

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


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

暂无
暂无

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

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