Search code examples
androidmultithreadingfirebasefirebase-cloud-messagingandroid-lifecycle

Can push notification execute code in parallel with UI thread or not?


AsFirebaseMessagingService does not use the Main Thread, I am just wondering as all my code in all of my activities or fragments run in UI thread(Main Thread). Now suppose my activity's onCreate method is executing and then I receive the push notification. Will these two blocks of code run in parallel, or will the push notification code wait in the queue until onCreate() method OR Activity's last life cycle method gets executed?

Edit- As you are saying code will run parallelly then suppose I have a variable in App.java

public class App extends Application {
    int ctr = 100;
}

StatusActivity.java

public class StatusActivity extends BaseActivity {
     public void onCreate() {
         fun();
     }
     public void fun() {
         int d = App.ctr - 1;//Step 1 Here d = 99

         int m = App.ctr - 1; // Step 3 Here m = 98

     }

}

FcmListener.java

 public class FcmListener extends FirebaseMessagingService {
     Override
     public void onMessageReceived(RemoteMessage mssg) {
         App.ctr = App.ctr - 1;//STEP 2 // Now App.ctr = 99

     }
 }

Now as you can see in the above code there will be problems if push notif code executes in parallel with fun(). I want push_notif and fun() to run serially, where order doesn't matter but not in parallel.


Solution

  • As already pointed out in a parallel answer, the overriden methods of FirebaseMessagingService run in a background thread, so you should use synchronization strategies in order to access/use mutable object from different thread.

    But the question I want to answer is a bit different. Let's for a moment assume, that overriden methods run on a main thread. So is there a possibility, that the order of execution will be STEP 1 then STEP 2 and then STEP 3?

    Android works with a technique called MessageQueue, basically there are Messages posted on that queue, on which Looper loops and "parses/executes" them.

    Now if we assume, that you are currently located on STEP 1, it means, that there was a particular Message which is currently being executed (hypothetically, let's assume that action is - perform onCreate() of this activity).

    Until this message is fully executed there cannot exist another Message which might get have a chance to be executed. So if we assume, that Firebase dispatches an event on background thread but the actual overriden method is being run on main thread, then this overriden method would have chance to be executed only after current Message (activity's onCreate()) has finished. In other words, there would be posted another Message on the MessageQueue, which would perform onMessageReceived() when the Looper will give chance for this message to be executed.

    So, theoretically, there is no chance that the ordering would be STEP 1 -> STEP 2 -> STEP 3.

    If STEP 1 is already executed, then it will continue with STEP 3 and the STEP 2 (at some point in future, because you can't know what other Messages are already posted on MessageQueue).

    See this article for more details about MessageQueue and related classes.