Rissmon Sure..
16
使用广播接收器并在您的活动中注册它,当新消息到来时会触发
简短的例子,在你的FirebaseMessagingService :: onMessageReceived中,你可能有:
cappello = remoteMessage.getNotification().getBody();
Intent intent = new Intent();
intent.putExtra("extra", cappello);
intent.setAction("com.my.app.onMessageReceived");
sendBroadcast(intent);
然后在MainActivity中,实现BroadcastReceiver:
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String state = extras.getString("extra");
updateView(state);// update your textView in the main layout
}
}
并在MainActivity的onResume()中注册它:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.my.app.onMessageReceived");
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver(receiver, intentFilter);