Search code examples
androidfirebaseandroid-servicefirebase-cloud-messagingandroid-intentservice

Does FirebaseMessagingService run in the background by default?


Does the FirebaseMessagingService run in the background similar to how an IntentService operates?

I see that FirebaseMessagingService extents Service which does not run in the background, but I'd like to be sure whether or not I should be doing any work inside the FirebaseMessagingService asynchronously or synchronously.


Solution

  • FirebaseMessagingService's method onMessageReceived(RemoteMessage message) is called "in the background" (not on the UI/Main thread). If you try do asynchronous work inside onMessageReceived(RemoteMessage message) you will receive an error saying:

    Method execute must be called from the main thread, currently inferred thread is worker.

    So all work that is done within onMessageReceived(RemoteMessage message) should be done synchronously because it's in its own background worker thread.