通知事件类 (POJO)
public class NotificationEvent {
LoggedInUserInterface liui;
public NotificationEvent(LoggedInUserInterface liui) {
this.liui = liui;
}
}
MyFirebaseMessagingService(订阅者)
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");
}
我的活动(海报)
EventBus.getDefault().postSticky(new NotificationEvent(this));
Log.d("notifyUser", "Activity post call done");
错误
EventBus: No subscribers registered for event class com.myCompany.myApp.NotificationEvent
No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
成功日志
Activity post call done
不成功的日志
EventBus call received in service
我试过粘和不粘都无济于事。帮助表示赞赏
更新
我可以确认事件总线工作正常,真正的问题是:该服务仅在收到通知时运行。这是一个问题,因为该服务在每次创建活动时都需要来自主活动的更新字符串,而不是仅在收到通知时。
可能的黑客解决方案
每次启动应用程序时,我都可以向自己发送通知(而不是显示它)。这将启动服务并允许事件总线更新字符串。这并不理想,并且会随着预算的增加而耗尽我的 Firebase 工资。因此,仍然会非常感谢本地解决方案。