How to communicate between Firebase Messaging Service and Activity? Android

In the onMessageReceived method you could send an in application broadcast, transferring the driver details. The request accepted method would then be replaced by the broadcast receiver onReceive method. That way there is no need to write to the shared preferences or use a aSyncTask which is basically doing nothing as the doinbackground method just returns null . You are only using the onPreExecute and onPostExecute methods, which are both executed on the main thread.


Why are you using AsyncTask? It doesn't make sense. Anyway, if you want to communicate with the Activity you can do it with BroadcastReceiver.

public class MyFirebaseMessagingService extends FirebaseMessagingService{
    private LocalBroadcastManager broadcaster;

    @Override
    public void onCreate() {
        broadcaster = LocalBroadcastManager.getInstance(this);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent = new Intent("MyData");
        intent.putExtra("phone", remoteMessage.getData().get("DriverPhone"));
        intent.putExtra("lat", remoteMessage.getData().get("DriverLatitude"));
        intent.putExtra("lng", remoteMessage.getData().get("DriverLongitude"));
        broadcaster.sendBroadcast(intent);
    }
}

and in your Activity

 @Override
    protected void onStart() {
        super.onStart();
        LocalBroadcastManager.getInstance(this).registerReceiver((mMessageReceiver),
                new IntentFilter("MyData")
        );
    }

    @Override
    protected void onStop() {
        super.onStop();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            awaitingDriverRequesting.dismiss();
            awaitingDriverPhone.setText(intent.getExtras().getString("phone")); //setting values to the TextViews
            awaitingDriverLat.setText(intent.getExtras().getDouble("lat"));
            awaitingDriverLng.setText(intent.getExtras().getDouble("lng"));
        }
    };

EDIT

According to @xuiqzy LocalBroadcastManager is now deprecated! Google says to use LiveData or Reactive Streams instead.


Use Eventbus for background communication. It is best.

Just throw an event from your onMessageReceived() function calling

Eventbus.getDefault().post(Throw(value))

Create an event model for your event. Every event model must be unique.

class Throw(val value :Object) {}

Then in your activity just register the desired function with your event model. It will receive when you fire an event. It is easy to implement and more understandable.

override fun onStart() {
    super.onStart()
    EventBus.getDefault().register(this)
}


override fun onStop() {
    EventBus.getDefault().unregister(this)
    super.onStop()
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onThrowEvent(t : Throw) {
    // Do your staff here
}

You can also catch your event in background. Just change the ThreadMode. I will suggest you to give it a try

@Subscribe(threadMode = ThreadMode.ASYNC)
@Subscribe(threadMode = ThreadMode.BACKGROUND)