Dylan
Dylan

Reputation: 397

Service (event bus subscriber) not receiving post from Activity

NotificationEvent Class (POJO)

public class NotificationEvent {
LoggedInUserInterface liui;
public NotificationEvent(LoggedInUserInterface liui) {
    this.liui = liui;
}

}

MyFirebaseMessagingService (subscriber)

LoggedInUserInterface liui;
@Override
public void onCreate() {
    super.onCreate();
    EventBus.getDefault().register(this);
}

@Override
public void onDestroy() {
    EventBus.getDefault().unregister(this);
    super.onDestroy();
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(NotificationEvent notificationEvent) {
    LoggedInUserInterface liui = notificationEvent.liui;
    this.liui = liui;
    Log.d("notifyUser", "EventBus call received in service");
}

My Activity (poster)

        EventBus.getDefault().postSticky(new NotificationEvent(this));
    Log.d("notifyUser", "Activity post call done");

Error

EventBus: No subscribers registered for event class com.myCompany.myApp.NotificationEvent
No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

Successful Log

Activity post call done

Unsuccessful Log

EventBus call received in service

I've tried sticky and non sticky to no avail. Help is appreciated

UPDATE

I can confirm that that the event bus works and the real problem is: The service only runs when a notification is received. This is a problem because The service requires an updated string from the main activity each time the activity is created, as opposed to only when a notification is received.

POSSIBLE HACKY SOLUTION

I could send myself a notification each time the app is started (and not display it). This would start the service and allow for the event bus to update the string. This is not ideal and would eat up my firebase pay as you go budget. So a local solution would still be greatly appreciated.

Upvotes: 0

Views: 1100

Answers (2)

Martin
Martin

Reputation: 4826

You might try taking off Thread =Main Srrbice should not be running on the main thread

Upvotes: 0

Fabio
Fabio

Reputation: 2824

1st,put a log on firebase service creation to make sure its really started. 2nd, if green works as otto you need to have the service running in the same process, or else they won't share the resources (threads, memory stack) that allow you to post messages between them.

And last, intents may be a better/more reliable way of communicating from activities towards services.

Upvotes: 1

Related Questions