Y. Vasquez
Y. Vasquez

Reputation: 3

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 !!

Upvotes: 0

Views: 2410

Answers (2)

mcamocci
mcamocci

Reputation: 56

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);

Upvotes: 1

Muhammad Younas
Muhammad Younas

Reputation: 1603

The Firebase Cloud Messaging Android Quickstart app demonstrates registering an Android app for Notifications and handling the receipt of a message. InstanceID allows easy registration while FirebaseMessagingService and FirebaseInstanceIDService enable token refreshes and message handling on the client.

Upvotes: 0

Related Questions