简体   繁体   English

仅在星期一进行本地通知

[英]Local notifications only on Monday

I am trying to send a local notifications on every Monday. 我正尝试在每个星期一发送本地通知。 Let say that I have a scenario in which I have to send a medication taking reminder on every Monday for one month. 假设我有一种情况,我必须在每个星期一发送一个服药提醒,持续一个月。 So it will be total 4 notification in a month. 因此,一个月内总共会有4条通知。 My code is as below but I can't figure out the following things; 我的代码如下,但是我无法弄清楚以下几点; 1)How to send the notifications on a specific day 2)How to limit the notification for a max end date. 1)如何在特定日期发送通知2)如何限制通知的最大结束日期。

The code for sending the notification is as follows; 发送通知的代码如下:

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) 

Can anyone please help? 谁能帮忙吗? Regards, neena 问候,娜娜

Add the NSWeekCalendarUnit to NSDateComponents and set the repeatInterval to NSWeekCalendarUnit. 将NSWeekCalendarUnit添加到NSDateComponents,并将repeatInterval设置为NSWeekCalendarUnit。 For Example : 例如 :

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;

You can manage something like, 您可以管理类似

   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