FirebaseMessagingService never works when the application is in the foreground. In this case, if you want to receive a message from FCM, then WakefulBroadcastReceiver will work for you
public class FirebaseDataReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("BroadcastReceiver::", "BroadcastReceiver"); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(intent.getExtras().getString("title")) .setContentText(intent.getExtras().getString("message")); NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); manager.notify(0, builder.build()); } }
Associate firebase with GCM in the play store and write the code below in the manifest
<receiver android:name=".firebase.FirebaseDataReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </receiver>
source share