8

I'm trying to find out what is actually possible on iOS when it comes to background tasks. I have an app which is running in the background and tracking location changes on the device.

Now, is it possible to send a push notification to the device and have the device send the current location to a server without needing the user to open the app so it runs in the foreground?

3 Answers 3

6

I know this question is a bit old but I'm answering for those who, like me, arrived here long after the question was asked.

Now, is it possible to send a push notification to the device and have the device send the current location to a server without needing the user to open the app so it runs in the foreground?

Yes. You can now do that. I'm currently doing something pretty similar in my app. There are some other SO questions that now correctly state this. This is one

Basically, you have to enabled Background Execution for Remote Notifications. You can do this in XCode by going to your app's target. Open the Capabilities tab enabling the Background Modes feature and checking the Remote Notifications item in the checklist. (I'm assuming you already have enabled the Push Notifications feature).

With this the code you use to handle notifications in foreground will also serve you in background as the application:didReceiveRemoteNotification:fetchCompletionHandler method will be called while your app is in background. Here's a sample code in Swift 3.0:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  // Print full message.
  print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

  // Print full message.
  print(userInfo)

  completionHandler(UIBackgroundFetchResult.newData)
}

You may wanna check out this SO question (and accepted answer) about how to handle network request in background. Background request not execute Alamofire Swift

Check out the docs about Background Execution. Particularly the section about Push Notifications

1
  • Could you please tell, is it necessary to enable push notification when receiving local notifications, when your app is in background? I am facing a problem
    – ArgaPK
    Apr 18, 2018 at 10:57
1

Unfortunately I don't think so. When your app is in a suspended (freezed) state, push notifications are managed by SO, users need to reopen the app. They other way around is to create local notification for significant changes, when the notif arrives your app has a minimum amount of time, it will be not enough to post data, but you can study something. I will suggest you to read this question it talks about bluetooth and geolocation link

-5

It's impossible. This OS has one principle —— user should know what you(your app) have done. When your app is suspended,your push message will be received by the notification center.The only way let your app know this thing happened is to launch your app through the notification center.so.....

2
  • Are you sure? According to the answer in this thread (stackoverflow.com/questions/15225677/…) its possible to send empty notifications. No alert or badge will show up and you can include some custom dictionary data. But what you're saying is that this data wont be delivered to the app until the app launches? My app is runing in the background monitoring location changes.
    – Trj
    Mar 8, 2013 at 7:50
  • No alert or badge notifications use to send other custom dictionary data to your app.your background work is also register from the system,when your app come to the background, the push message can not received,case your notification is register from the system,too.
    – junkor
    Mar 8, 2013 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.