2016-05-16 53 views
0
let dateComponents1 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
    dateComponents1.month = 5 
    dateComponents1.day = 12 
    dateComponents1.hour = 20 
    dateComponents1.minute = 00 
    let notification1 = UILocalNotification() 
    notification1.alertAction = "notification1" 
    notification1.alertBody = "notification1" 
    notification1.repeatInterval = NSCalendarUnit.WeekOfYear 
    notification1.fireDate = calendar!.dateFromComponents(dateComponents1) 
    UIApplication.sharedApplication().scheduleLocalNotification(notification1) 




    let dateComponents2 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
    dateComponents2.month = 5 
    dateComponents2.day = 13 
    dateComponents2.hour = 14 
    dateComponents2.minute = 00 
    let notification2 = UILocalNotification() 
    notification2.alertAction = "notification2" 
    notification2.alertBody = "notification2" 
    notification2.repeatInterval = NSCalendarUnit.WeekOfYear 
    notification2.fireDate = calendar!.dateFromComponents(dateComponents2) 
    UIApplication.sharedApplication().scheduleLocalNotification(notification2) 

我想在每週的特定時間發出2個通知。這是我如何實施通知。每週星期四和星期五下午8點發出2個通知。我做對了嗎?有時我會在火災時收到重複通知。迅速多個通知

+0

你在哪裏調用這段代碼?你在打didFinishLaunching嗎? – Harish

+0

我從didBecomeActive –

回答

0

好的,所以在你提到的評論中,你在didBecomeActive中調用了這段代碼。與此相關的問題是,每當您的應用程序變爲活動狀態時,您都會安排您不需要的重複通知。因此,您應使用此代碼:

let dateComponents1 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
dateComponents1.month = 5 
dateComponents1.day = 12 
dateComponents1.hour = 20 
dateComponents1.minute = 00 
let notification1 = UILocalNotification() 
notification1.alertAction = "notification1" 
notification1.alertBody = "notification1" 
notification1.repeatInterval = NSCalendarUnit.WeekOfYear 
notification1.fireDate = calendar!.dateFromComponents(dateComponents1) 
UIApplication.sharedApplication().cancelLocalNotification(notification1) 
UIApplication.sharedApplication().scheduleLocalNotification(notification1) 




let dateComponents2 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) 
dateComponents2.month = 5 
dateComponents2.day = 13 
dateComponents2.hour = 14 
dateComponents2.minute = 00 
let notification2 = UILocalNotification() 
notification2.alertAction = "notification2" 
notification2.alertBody = "notification2" 
notification2.repeatInterval = NSCalendarUnit.WeekOfYear 
notification2.fireDate = calendar!.dateFromComponents(dateComponents2) 
UIApplication.sharedApplication().cancelLocalNotification(notification2) 
UIApplication.sharedApplication().scheduleLocalNotification(notification2) 

什麼,所以你不要我在這裏做的是取消先前的通知(即原定的最後一次應用程序變得活躍的一個),然後調度一遍有太多重複的通知。此方法的替代方法是將代碼放置在只被調用一次的地方,以免發生重複通知。

+0

調用它,你剛纔說過把代碼放在別的地方。那麼最好的地方是什麼? –

+0

一切都取決於場景......您可以在第一次應用程序發佈時執行此操作。所以第一次用戶啓動應用程序時,通知將被設置。請參閱http://stackoverflow.com/questions/9964371/how-to-detect-first-time-app-launch-on-an-iphone瞭解如何檢測首次啓動 – Harish

+0

另外,如果您在首次啓動應用程序時執行此操作代碼只會被調用一次,所以你不會得到任何重複的通知。 – Harish