0

I am struggling to get a local notification to run using background fetch. Ideally what I need is to update my JSON data and if it contains a value then send a local notification to the user.

as yet I am unable to even send a local notification using background fetch.

I have checked out the following: Send Local Notifications while App is running in the background Swift 2.0

ios call function in background mode

and a tutorial https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started

I am still unable to send even a notification let alone do my json query in the background to check for a value.

here is my code for appdelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)


    return true
}

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let vc = window?.rootViewController as? FirstViewController {
        vc.mynotifications()

    }
}

and my notification code from the view controller:

func mynotifications() {
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.subtitle = "subtitle"
    content.body = "test"
    content.badge = 1
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.5, repeats: false)
    let request = UNNotificationRequest(identifier: "timerdone", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

I have background fetch enabled in my settings.

Any assistance in getting this to work would be appreciated. If you could also point me in the right direction to tie in the json request in the background that would also be a bonus.

Thanks

confusedChris
  • 93
  • 1
  • 4
  • 8
  • 1
    Have you tried adding breakpoints or `NSLog()` statements to see if any of your methods are being called? – Craig Otis Mar 07 '18 at 12:41
  • Expanding on what @CraigOtis said, be more specific. What does "unable to send a notification" mean? Does it mean `mynotifications()` is never being called, or does it mean `performFetchWithCompletionHandler()` isn't being called? See what your debugger and some judiciously placed `NSLog()` or `print()` calls can tell you, and if you're still stuck after that, come back and update your question to show everyone your results--it will provide clues to enable people to help you better. – SaganRitual Mar 07 '18 at 14:57
  • 1
    I would suggest reviewing the documentation for `performFetchWithCompletionHandler`. In that method *you* start the background fetch, and call the completion handler when it is done. You must also enable background fetch and notifications in your UIBackgroundModes. And local notifications, as far as I know, cannot be used to trigger a background fetch. You can try setting `content-available` in the notification content, but this is not likely to actually work with a local notification. – quellish Mar 07 '18 at 19:08
  • Did you got it working? I'm sending a silent notification to my app, and triggering a local notification. It works when my app is on foreground and background, but if I kill my app, it won't work. Any idea why it is not working? – Jeremias Serafim Nov 15 '18 at 05:10

0 Answers0