javaandroidfirebasenotificationslocalbroadcastmanager

how handle fcm notification data without need to click on notification popup in android


i used LocalBroadcastManager and pass data through it in onmessagerecieved()

  if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        Intent i=new Intent("com.taskty.tasktysupplierapp_FCM_MESSAGE");
        String orderid=remoteMessage.getData().get("orderid");
        String placeOfExecution=remoteMessage.getData().get("placeOfExecution");
        i.putExtra("orderid",orderid);
        i.putExtra("placeOfExecution",placeOfExecution);
        LocalBroadcastManager lbm=LocalBroadcastManager.getInstance(this);
        lbm.sendBroadcast(i);


        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }
    }

and then call it in the launcher activity as

private BroadcastReceiver mhandler=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String orderid = intent.getStringExtra("orderid");
        Toast.makeText(context, "order id is :"+orderid, Toast.LENGTH_SHORT).show();

    }
};

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mhandler);
}

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mhandler,new IntentFilter("com.taskty.tasktysupplierapp_FCM_MESSAGE"));
}

and register receiver in onCreate() also before setcontentview()


Solution

  • I found the answer by override the following method it works in all cases whether app in background or foreground and you can get data from intent

      @override
      onhandleIntetn(Intent intent){}