Ad

How To Create Event Listener When Message From Firebase Received In Android?

I am using firebase to receive messages. When message received (onMessageReceived()). I want to check whether the message contains required string (say "hello"). If yes, i want it to be known to another fragment, so that some method in fragment will be called based on message.

For this i want to create event listener when message received. In my fragment, two APIs will be called. on API will be called onCreatedView(). based on message from firebase , second API should be called. The two API calls should be done within fraction of milliseconds.

MyFireBaseMessage.java

public class MyMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d("message here","message here "+ remoteMessage.getData().toString());
    String value = remoteMessage.getData().get("msg");
    Log.d("value here","value here"+" "+value);
    if (value.equalsIgnoreCase("hello"))
    {
        //implement custom event listener here
    }
}
}
Ad

Answer

One way I can think of right now is using LocalBroadcast. to do this

  1. Amend your Firebase service and put this content

    public class MyMessagingService extends FirebaseMessagingService {
    private LocalBroadcastManager broadcaster;
    
    @Override
    public void onCreate() {
        broadcaster = LocalBroadcastManager.getInstance(this);
    }
    
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("message here", "message here " + remoteMessage.getData().toString());
        String value = remoteMessage.getData().get("msg");
        Log.d("value here", "value here" + " " + value);
        if (value.equalsIgnoreCase("hello")) {
            Intent intent = new Intent("custom-listener");
            broadcaster.sendBroadcast(intent)
        }
    }
    

    }

  2. Make changes in your fragment class

public class BlankFragment extends Fragment {

    Context context;
    BroadcastReceiver br;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onResume() {
        super.onResume();
        setup();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LocalBroadcastManager.getInstance(context).unregisterReceiver(mMessageReceiver);
    }

    private void setup() {
        LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
                new IntentFilter("custom-listener"));
    }


    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Got message: " + message);
        }
    };

}

with broadcasts you will be able to listen to the event you want to. The onReceive method inside the Fragment class will get fired when your if condition is true i.e. value.equalsIgnoreCase("hello")

Apologies for the formatting of this answer. having difficulties adjusting it.

Ad
source: stackoverflow.com
Ad