Does FirebaseMessagingService run in the background by default?

懵懂的女人 提交于 2019-11-28 04:07:47

问题


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.

Thank you


回答1:


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.




回答2:


A Service does not "run in the background". A Service is just an instance of a class (ie: an object). The methods of a Service can run on either the main (UI) thread or on a background (worker) thread. It all depends on how they are called.

The lifecycle methods of the service onCreate(), onStartCommand(), onDestroy() are all called on the main (UI) thread. But in your Service you can start other threads and do other things on those threads.



来源:https://stackoverflow.com/questions/41067081/does-firebasemessagingservice-run-in-the-background-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!