Search code examples
androidfirebasenotificationsfirebase-notificationsgreenrobot-eventbus

How to receive eventbus events when Activity is in the background


I want to get notification message using Firebase Notification service. I'm sending message from Firebase, it is ok.

I want to get this notification if user run in MainActivity also I want to show pop-up using dialog.

If user run other activities for example SettingActivity or ProfileActivity, notification handle anyway and user run MainActivity pop-up appear suddenly.

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.

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

@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);
}

Solution

  • You're calling unregister() in onStop(), so you do not receive events when MainActivity is in the background.

    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()).

    Move the following line to onCreate():

    EventBus.getDefault().register(this);
    

    And this one to onDestroy():

    EventBus.getDefault().unregister(this);
    

    Also check out the Activity Lifecycle.