0

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.

Dylan
  • 375
  • 1
  • 2
  • 12

2 Answers2

1

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.

Fabio
  • 2,654
  • 16
  • 31
  • I can't send data using intents because I can't override the service's onStartCommand() - it's a final method. I added the log in onCreate(): The Log did NOT print in LogCat. I think the service is only started when a message/notification is received from firebase. – Dylan Dec 01 '19 at 03:21
  • Now this sounds like a tricky one - and I can't get to a computer to read the docs now. What are you most interested in, a) knowing when something happens to take an action, or b) sending data so the service can use it when it sees fit? Traditionally you can solve b by using a singleton that holds that data, in some cases it would be appropriate to call it XyzRepository. Now if what you want is a, try hard to put that logic outside the service, seems like a code smell to me. – Fabio Dec 01 '19 at 03:28
  • Ideally I would like to send an interface object from from the activity to the service, enabling the service to call methods within the activity. At minimum I would like to send a string from the activity to the service. I'm more interested in B. – Dylan Dec 01 '19 at 03:37
  • Here's 2 things activities shouldn't do : 1-hold values so other objects can get the latest, and 2- have methods that other objects can call. Although 2 often happens for complicated reasons, I don't think it's your case. Here's how I would do it : a XManager class that runs the logic and a XRepository that stores data. Activity sends data to repo who optionally writes it to Shared Preferences. Service calls method from X Manager who reads from repository. It's a much more flexible architecture and avoids problems particular to Activities, services and message busses. I hope it makes sense. – Fabio Dec 01 '19 at 04:17
  • Two Questions: What is Shared Preferences? How would the service call a method in X manager? – Dylan Dec 01 '19 at 04:25
  • SharedPreferences will allow you to store data even when your phone reboots- it writes /reads to disk. And if you split responsibilities right, XManager will only contain code and one reference to the above mentioned SharedPrefreence - preferably as a constructor argument. Which means that all instances of it are indistinguishable, because one class holds data and the other code. Simply instantiate XManager whenever you want it and the right architecture will make sure everything woks correctly. This is similar to server stateless architecture where Shared preference is a similar to a database – Fabio Dec 01 '19 at 04:34
0

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

Martin
  • 4,711
  • 4
  • 29
  • 37
  • Good point, I changed the thread mode to POSTING - the default. Please read the update in the question, as I have narrowed down the problem. – Dylan Dec 01 '19 at 03:54