首页 文章

仅在周一发送本地通知

提问于
浏览
0

我想在每个星期一发送本地通知 . 假设我有一个场景,我必须在每个星期一发送药物提醒一个月 . 所以一个月内会有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

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

    你可以管理类似的东西,

    var notification = UILocalNotification()
    
        notification.fireDate! = fireDate // this should be monday with desired timr
    
    
        notification.repeatInterval = NSWeekCalendarUnit  //this will repeat every week
    

相关问题