0

I have created an iOS app that sends a local notification daily at 11:00 AM.

I have followed this!

and my code is...

func setNotification(){

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var dateFire=NSDate()

    var fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

    if (fireComponents.hour >= 11) {
        dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

        fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())
    }


    fireComponents.hour = 11
    fireComponents.minute = 00

    dateFire = calendar.dateFromComponents(fireComponents)!

    let localNotification = UILocalNotification()
    localNotification.fireDate = dateFire
    localNotification.alertTitle = "Title"
    localNotification.alertBody = "Body"
    localNotification.repeatInterval = NSCalendarUnit.Day

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

Now the app works good and notifications are sent at 11:00 AM daily but the problem is they are incrementing by one daily :(

Yesterday I had received 6 notifications and today I received 7 at 11:00am

Please help me to get out of this issue.

Thanks!

Community
  • 1
  • 1
Zeero0
  • 2,602
  • 1
  • 21
  • 36
  • Possible duplicate of [Receiving duplicate push notification ios9](http://stackoverflow.com/questions/32840916/receiving-duplicate-push-notification-ios9) – Marcus Adams Sep 08 '16 at 12:23

1 Answers1

0

I tried this and its working. You are checking if condition and overwriting firecomponents so it is of no use.

let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var dateFire=NSDate()

let fireComponents = calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

fireComponents.hour = 7
fireComponents.minute = 41
fireComponents.second = 00

dateFire = calendar.dateFromComponents(fireComponents)!

let localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "Body"
localNotification.repeatInterval = NSCalendarUnit.Minute

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

For further details https://wordpress.com/post/iosmygirlfriend.wordpress.com/48

laxman khanal
  • 998
  • 1
  • 8
  • 18
  • Thanks for your time. Yeah it is working good but if condition is used, if someone missed a notification then it will not be sent that day, and will be scheduled at exact time of next day – Zeero0 Sep 08 '16 at 14:31