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

时间:2017-11-07 13:58:16

标签: java android firebase notifications localbroadcastmanager

我使用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()注册接收器

1 个答案:

答案 0 :(得分:1)

我通过覆盖以下方法找到了答案,它适用于所有情况,无论是在后台还是前景中的app,你都可以从意图中获取数据

  @override
  onhandleIntetn(Intent intent){}