从Firebase收到消息后,如何在应用程序中更改视图?

时间:2018-12-02 08:53:48

标签: android firebase-cloud-messaging

是否有可能在从onMessageReceived(RemoteMessage remoteMessage)(FirebaseMessagingService)的firebase接收消息后,如果正在运行,则重新加载该应用程序的当前视图?例如我已经启动MainActivity,并且从Firebase收到消息,我想在MainActivity中重新加载ListView。

1 个答案:

答案 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()

中的 unregisterReceiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(
                mMessageReceiver);