2017-01-05 41 views
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

+1

你可能會想安排多個通知,一個是要每一個具體的日期。 – Paulw11

+0

修復週一發佈的任何日期,然後將timeInterval設置爲每週。 notification.repeatInterval = NSWeekCalendarUnit; –

回答

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 
+0

以及如何在一個月後結束此通知? – neena

+0

UIApplication.shared.cancelLocalNotification(通知) – Lion