Execute code in background when push notification is received
Asked Answered
K

3

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?

Korman answered 8/3, 2013 at 7:20 Comment(0)
A
7

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

Arnold answered 8/4, 2017 at 2:15 Comment(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 problemIrritate
A
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

Amperehour answered 8/3, 2013 at 7:33 Comment(0)
S
-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.....

Successive answered 8/3, 2013 at 7:36 Comment(2)
Are you sure? According to the answer in this thread (#15226177) 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.Korman
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.Successive

© 2022 - 2024 — McMap. All rights reserved.