Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

FCM message to start a service in background

I can currently run a location polling class to let the server know where a device is on FCM receipt, but only while the App is open it runs through the class perfectly. While the phone is not on main screen I can only send the push notification through SendNotification(..) within the override void OnMessageReceived(..) class. I would like to call specific location class to run while the App is in the background. Would I do this using a service? How can I run specific code from that point? I tried it to log to a text file but it does nothing, just displays the push notification.
Would I try to call the method through an intent on FCM receipt?

Any guidance would be appreciated!

Best Answer


  • JacquesOkesJacquesOkes Member ✭✭

    Thank you for your response and time.

    I have now officially implemented the foreground service from the link you sent and I managed to get a service to start. However, I can only get the service to run when the App is open. When I press to "o" button and go to Android home screen, and then send a firebase message to the device it does not run the foreground service at all.

    I read up about something relating to a wake lock of some kind?

    I am implementing 2 versions of the service, one for Android 7 specifically (As the app is targeted at really cheap devices for a specific industry) and also for android 8 an above for those who do have better devices.

    On both Android 7 service and the 8+ service runs in the front end without issue. Android 7 I am calling "StartService(myService);" and on 8+ I am calling "StartForegroundService(myService);"

    Just to get the service running now without the app being open would be great.

    I also saw some things on background services, would I not possibly need something like that?

    Thanks again for you insight that has really helped!

  • JacquesOkesJacquesOkes Member ✭✭

    I do see that it is possible to raise an event when you click on the push notification that displays, but is there a way to bypass the user having to click the notification? So the intent service starts then as the FCM is received.

  • JacquesOkesJacquesOkes Member ✭✭

    Solved with the biggest issue being the data payload being sent via FCM was a notification. A notification does not fire the OnMessageReceived event in Xamarin. Once that was changed to data payload only the event fired and I could get the foreground service running from that.

    Thanks again for your help and guidance which led me to research until I found the solution.

  • junniornetjunniornet PEMember

    Can you share your code please? Specificly the service code. Im searching this solution but I dont found it :neutral: I await your help !

  • JacquesOkesJacquesOkes Member ✭✭

    The problem was actually with the FCM being sent - that needs to have a data payload, I only sent a message as text and it wasn't picking it up when the app was minimized.

    Check out this tutorial - this should answer everything you need to know.

    They are just sending the firebase message directly from the it would work the same as sending it from a service or other application...if after you go through all this you still are not coming right, send me a git link and I can have a look see whats going on there, but keep at it, you will get it!

Sign In or Register to comment.