当Activity在后台时如何接收事件总线事件 [英] How to receive eventbus events when Activity is in the background
问题描述
我想使用Firebase通知服务获取通知消息.我正在从Firebase发送消息,没关系.
I want to get notification message using Firebase Notification service. I'm sending message from Firebase, it is ok.
如果用户在MainActivity
中运行,我想获得此通知,我也想使用对话框显示弹出窗口.
I want to get this notification if user run in MainActivity
also I want to show pop-up using dialog.
如果用户运行其他活动(例如SettingActivity
或ProfileActivity
),则无论如何都会有通知句柄,并且用户运行MainActivity
的弹出窗口会突然出现.
If user run other activities for example SettingActivity
or ProfileActivity
, notification handle anyway and user run MainActivity
pop-up appear suddenly.
为此,我正在使用Greenbot Eventbus.当我进入MainActivity
并收到通知时,它就可以了.但是当我进入另一个Activity
通知时,不会出现.
To do this I'm using Greenbot Eventbus. When I'm inside MainActivity
and notification comes It appears so it is ok. But When I'm inside another Activity
notification not coming.
在MainActivity
出现之前如何处理此消息?
How to handle this message until come MainActivity
?
public class NotificationService extends FirebaseMessagingService {
private static final String TAG = "evenBus" ;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "onMessageReceived");
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
// do nothing if Notification message is received
Log.d(TAG, "Message data payload: " + remoteMessage.getNotification().getBody());
String body = remoteMessage.getNotification().getBody();
EventBus.getDefault().post(new NotificationEvent(body));
}
}
}
MainActiviy
MainActiviy
@Override
protected void onResume(){
EventBus.getDefault().register(this);
}
// This method will be called when a MessageEvent is posted (in the UI thread for Toast)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(NotificationEvent event) {
Log.v("onMessageEvent","Run");
Toast.makeText(MainActivity.this, event.getBody(), Toast.LENGTH_SHORT).show();
alertSendActivity("title",event.getBody());
}
@TargetApi(11)
protected void alertSendActivity(final String title,final String data) {
alt = new AlertDialog.Builder(this,
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create();
alt.setTitle(title);
alt.setMessage(data);
alt.setCanceledOnTouchOutside(false);
alt.setCancelable(false);
alt.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
alt.dismiss();
}
});
alt.show();
}
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
推荐答案
您正在onStop()
中调用unregister()
,因此当MainActivity
在后台时您不会收到事件.
You're calling unregister()
in onStop()
, so you do not receive events when MainActivity
is in the background.
要接收事件,即使Activity
在后台,也应在onCreate()
中注册,然后在onDestroy()
中注销(而不是在onResume()
/onStop()
中).
To receive events even when the Activity
is in the background, you should register in onCreate()
and unregister in onDestroy()
(rather than in onResume()
/onStop()
).
将以下行移动到onCreate()
:
EventBus.getDefault().register(this);
然后将其发送到onDestroy()
:
EventBus.getDefault().unregister(this);
还请查看活动生命周期.
这篇关于当Activity在后台时如何接收事件总线事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!