我使用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()
注册接收器
答案 0 :(得分:1)
我通过覆盖以下方法找到了答案,它适用于所有情况,无论是在后台还是前景中的app,你都可以从意图中获取数据
@override
onhandleIntetn(Intent intent){}