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)

I am trying to have 2 notifications fired at specific time every week. This is how I implement the notifications. 2 notifications fire 8 pm Thursday and Friday 2 pm every week. Am I doing it right? Sometimes I receive duplicate notifications at fire time.

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
彭章龙
  • 13
  • 2

1 Answers1

0

Ok, so in the comments you mentioned that you call this code in the didBecomeActive. The issue with that is every time your application becomes active you schedule a duplicate notification which you don't want. As a result you should use this code:

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)

What I am doing here is cancelling the previous notification (the one that was scheduled the last time the application became active) and then scheduling it again so you don't have too many duplicate notifications. An alternative to this method is placing your code somewhere where it only gets called once so you don't get duplicate notifications.

Harish
  • 1,374
  • 17
  • 39
  • you just said place the code somewhere else. Then what would be the best place to put? – 彭章龙 May 16 '16 at 01:44
  • All depends on the scenario... You could do it on first app launch. So the first time the user launches the app the notifications will be set up. See http://stackoverflow.com/questions/9964371/how-to-detect-first-time-app-launch-on-an-iphone to understand how to detect first launch – Harish May 16 '16 at 01:49
  • Also if you do it on first app launch the code will only be called once so you wouldn't get any duplicate notifications. – Harish May 16 '16 at 01:50
  • fixed. Thanks for ur help. – 彭章龙 May 16 '16 at 02:42
  • No problem :). If I fixed your problem could you click the check mark next to my answer to mark it as correct? – Harish May 16 '16 at 02:44