0

I want my app to schedule a local notification after some seconds it has entered in the background or inactive mode. This is whatIhave done in my view controller, when app enters background mode from it:

class HomeViewController: UITableViewController {
  override func viewDidLoad() {
    ...
    let notificationCenter:NSNotificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: #selector(self.appMovedToBackground), name: UIApplicationWillResignActiveNotification, object: nil)
  }

  func appMovedToBackground() {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    for userInfo in appDelegate.userInfoNotificationPending {
        let localNotification = UILocalNotification()
        localNotification.userInfo = userInfo
        localNotification.soundName = UILocalNotificationDefaultSoundName
        localNotification.alertBody = userInfo["aps"]!["alert"] as? String
        localNotification.fireDate = nil
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC))
        dispatch_after(time, dispatch_get_main_queue()) {
           UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
           UIApplication.sharedApplication().applicationIconBadgeNumber += 1
        }
    }
  }
}

why doesn't it work? The didReceiveLocalNotification method in app delegate is called at once as soon as the app enters background. Where am I making the mistake?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • 5
    I think,You need to set fireDate of notification like localNotification.fireDate = NSDate(timeIntervalSinceNow: 60) – dev_binod Mar 30 '16 at 13:57
  • That was right! I had understood that `fireDate` another thing, in other words I thought it was a time after which the notification was disposed if not read – SagittariusA Mar 30 '16 at 14:02

0 Answers0