java - 服务(事件总线订阅者)未收到来自 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 工资。因此,仍然会非常感谢本地解决方案。
解决方案
1st,在firebase服务创建上记录一个日志,以确保它真的开始了。2,如果绿色作为 otto 工作,您需要让服务在同一个进程中运行,否则它们将不会共享允许您在它们之间发布消息的资源(线程、内存堆栈)。
最后,意图可能是一种更好/更可靠的从活动到服务的通信方式。
推荐阅读
- javascript - 我有 2 个状态,但即使我只 setState 其中一个,它们都在更新
- python - 任何可用于在 Python 中开发移动应用程序的框架?
- azure - Azure 认知搜索引发 System.NullReferenceException
- php - 如何在特征中键入提示“$this”属性
- flutter - 未找到 Flutter 网络图像图像
- android - ItemTouchHelper.Callback.getDefaultUIUtil().onDraw() 改变 z-index
- c# - 如何使用启动表单应用程序打开 API 控制器?
- node.js - 如何在自定义 Jest 节点环境中使用 ES6?
- python - 如何查找用户在服务器 Discord.py 中发送的消息数
- c# - Entity Framework Core 迁移问题与动态连接字符串取决于标头值和另一个数据库