是否有可能在从onMessageReceived(RemoteMessage remoteMessage)(FirebaseMessagingService)的firebase接收消息后,如果正在运行,则重新加载该应用程序的当前视图?例如我已经启动MainActivity,并且从Firebase收到消息,我想在MainActivity中重新加载ListView。
答案 0 :(得分:1)
您可以在 onMessageReceived 中使用LocalBroadcastManager。例如:
Intent bIntent = new Intent("custom-event-name");
bIntent.putExtra("keyAction", "yourPushAction");
LocalBroadcastManager.getInstance(this).sendBroadcast(bIntent);
,并且可以在您活动的 onCreate()中接收广播,如下所示:
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("custom-event-name"));
和 mMessageReceiver 如下:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("keyAction");
}
};
不要忘记在 onDestroy()
中的 unregisterReceiverLocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);