Y. Vasquez Y. Vasquez - 5 months ago 120
Android Question

How to implement Firebase Cloud messaging with Foreground application?

How to receive a message in an Activity with the application in the foreground and display the message with Toast? I receive notification only when the app is in the background.

Something like messages in real time.

Please help me !!

Answer

On the oncreate method of activity to display toast.write.

registerReceiver(new MyReceiver(),new IntentFilter("MyReceiver"));

then create a MyReceiver BroadCastReceiver as the inner class in that Activity. as.

public class MyReceiver extends BroadCastReceiver{
    public void onReceive(Context context, Intent intent){
        Toast.makeText(context,intent.getStringExtra("from")+"         "+intent.getStringExtra("message"),Toast.LENGTH_SHORT).show();
    }

}
    ///finally you have to write the following codes on the 
    onMessageReceived
    Intent intents=new Intent();
    intents.setAction("MyReceiver");
    intents.putExtra("message",message.getData().get("message"));
    intents.putExtra("from",message.getData().get("from"));
    getBaseContext().sendBroadcast(intents);