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. So
Solution 1:
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.
publicclassMyReceiverextendsBroadCastReceiver{
publicvoidonReceive(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);
Solution 2:
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.
Post a Comment for "How To Implement Firebase Cloud Messaging With Foreground Application?"