如何处理 fcm 通知数据而无需单击 android 中的通知弹出窗口

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

我使用 LocalBroadcastManager 并在 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();
        }
    }

然后在启动器中调用它 activity 作为

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

并在 onCreate() 中注册接收者也在 setcontentview()

之前

我通过覆盖以下方法找到了答案,它在所有情况下都有效,无论应用程序在后台还是前台,您都可以从意图中获取数据

  @override
  onhandleIntetn(Intent intent){}