Background Task After Receiving A Notification

Right after I receive a notification I want to execute a background task. I'm using firebase to receive notification. I can receive notification successfully with 'UNUserNotificationCenter' in the AppDelegate file.

Now my goal is to send a "GET" - "PUT" request, right after I received a notification (only when the app is in background)

I saw the documentation about Background Tasks: https://developer.apple.com/documentation/backgroundtasks

But I can't found a way to execute a background task right after I receive a notification.

Is anyone has any suggestion about that? Thank you

Answered by Antoinette in 718005022

Thank you for your answer. I made it work

In app delegate:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],

                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        // Here return .newData after you checked if you have any new data, or .noData in case of nothing to update.
       // A request to the server can be made to check if new data are available for the app

        completionHandler(UIBackgroundFetchResult.newData)

    }

For those who are using firebase push notification, I use this: ["to" : token, "priority": "normal", "content_available": true] for silent notification

Now my goal is to send a "GET" - "PUT" request, right after I received a notification (only when the app is in background)

You have two choices here:

  • If you are concerned about latency, run the request in a standard session and use a UIApplication background task to prevent the app from suspending while it’s running.

  • If not, run the request in a URLSession background session. The system will run the request at a convenient time and resume your app in the background when it’s done.

For more information about UIApplication background tasks, see UIApplication Background Task Notes.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for your answer. I realized I didn't explain myself good enough, sorry. I'm not trying to continue a request in the background. In the normal situation, when the user is on the app and received a notification, my app will automatically send a request to the user's smart home device. This is working. Now, my goal is when the user doesn't use the app, so this one is not opened (background), when the phone receive the notification from my app, I want to send the same request to the user's smart home device.

So when the app is in background I still want to be able to send a request based on the phone receive my app notification. Is it something possible?

I still want to be able to send a request based on the phone receive my app notification

How are these notifications being generated? Based on your first post, I presume that these are push notifications. Is that correct?

If so, is your question:

  • How do I set things up to run my app in the background when the notification arrives?

  • Or, my app is running in the background based on such a notification, so how do I issue a network request?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

yes they are push notifications.

So yes, my question is, how do I set things up to run my app in the background when the notification arrives? Thank you

how do I set things up to run my app in the background when the notification arrives?

See Pushing Background Updates to Your App.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Thank you for your answer. I made it work

In app delegate:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],

                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        // Here return .newData after you checked if you have any new data, or .noData in case of nothing to update.
       // A request to the server can be made to check if new data are available for the app

        completionHandler(UIBackgroundFetchResult.newData)

    }

For those who are using firebase push notification, I use this: ["to" : token, "priority": "normal", "content_available": true] for silent notification

Background Task After Receiving A Notification
 
 
Q