首页 > 解决方案 > 服务(事件总线订阅者)未收到来自 Activity 的帖子

问题描述

通知事件类 (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 工资。因此,仍然会非常感谢本地解决方案。

标签: javaandroidevent-busgreenrobot-eventbus-3.0

解决方案


1st,在firebase服务创建上记录一个日志,以确保它真的开始了。2,如果绿色作为 otto 工作,您需要让服务在同一个进程中运行,否则它们将不会共享允许您在它们之间发布消息的资源(线程、内存堆栈)。

最后,意图可能是一种更好/更可靠的从活动到服务的通信方式。


推荐阅读