-1

I scheduled a local notification that should be activated after several hours without the user using the app. The problem I am having is that once it fires, the notification is repeated constantly until the app goes on the foreground. I would like for it to appear once only.

func applicationDidEnterBackground(application: UIApplication) {

        localNotification.fireDate = NSDate(timeIntervalSinceNow: (86400)*3)
        localNotification.alertTitle = "Te Extrañamos"
        localNotification.alertBody = "No olvides tu estudio en Momentos de Tora"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)     
    }

func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        UIApplication.sharedApplication().cancelLocalNotification(localNotification)
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

Thanks in advance for your help.

Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75

1 Answers1

0

All you need to do is keep track of you have sent the notification yet. Save a variable like let sentNotification: bool = false in your method before you send the notification check this variable. Then inside that check set it to true. In the applicationDidEnterForground method set it to false. So it will fire again when the parameters are reached.

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32