How can I run a code in background service when a push notification is received on Xamarin Forms

Viewed 170

I'm needing help. I need to execute a job (send to the server the UserId and the user location) even if the app is closed. I have tried many different approaches but I couldn't make it work.

It's an Xamarin forms app (android & iOS). I'm focused on Android side. I have a push notification service working (FCM using the azure-NotificationHub) if necessary for the proposed solution.

1 Answers

If you need this task executed at a regular cadence, I would recommend adding a service to your application that will run in the background and largely be separated from the state of the rest of your application. You can find some documentation on that here: Run Background Service - Android.

You mentioned that you have a Notification Hub if necessary - essentially the Android Client SDK for Notification Hubs will be creating a service like the one linked above and listening for events to trigger certain actions. If you need data at random (i.e. not at a regular cadence) this could be a good approach. However, assuming you just want an updated location every few minutes, it's probably simpler to just run your own background service.

Related