Activity 在后台时如何接收事件总线事件
How to receive eventbus events when Activity is in the background
我想使用 Firebase 通知服务获取通知消息。我正在从 Firebase 发送消息,没问题。
如果 MainActivity
中的用户 运行 我想收到此通知我还想使用对话框显示弹出窗口。
如果用户 运行 有其他活动,例如 SettingActivity
或 ProfileActivity
,通知句柄和用户 运行 MainActivity
弹出窗口突然出现。
为此,我使用了 Greenbot Eventbus。当我在 MainActivity
里面时,通知来了 它似乎没问题。但是当我在另一个 Activity
里面时,通知没有出现。
在 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));
}
}
}
主要活动
@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
在后台时您不会收到事件。
要在 Activity
处于后台时接收事件,您应该在 onCreate()
中注册并在 onDestroy()
中取消注册(而不是在 onResume()
/onStop()
).
将以下行移动到 onCreate()
:
EventBus.getDefault().register(this);
还有这个给onDestroy()
:
EventBus.getDefault().unregister(this);
另请查看 Activity Lifecycle。
我想使用 Firebase 通知服务获取通知消息。我正在从 Firebase 发送消息,没问题。
如果 MainActivity
中的用户 运行 我想收到此通知我还想使用对话框显示弹出窗口。
如果用户 运行 有其他活动,例如 SettingActivity
或 ProfileActivity
,通知句柄和用户 运行 MainActivity
弹出窗口突然出现。
为此,我使用了 Greenbot Eventbus。当我在 MainActivity
里面时,通知来了 它似乎没问题。但是当我在另一个 Activity
里面时,通知没有出现。
在 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));
}
}
}
主要活动
@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
在后台时您不会收到事件。
要在 Activity
处于后台时接收事件,您应该在 onCreate()
中注册并在 onDestroy()
中取消注册(而不是在 onResume()
/onStop()
).
将以下行移动到 onCreate()
:
EventBus.getDefault().register(this);
还有这个给onDestroy()
:
EventBus.getDefault().unregister(this);
另请查看 Activity Lifecycle。