- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从我的 GCMServiceListener
向我的 Activity
发送更新,所以,我正在使用 RxJava/RxAndroid
并创建了一个 BusClass
用于处理发送和 Observers
public class ClientBus {
//private final PublishSubject<Object> _bus = PublishSubject.create();
// If multiple threads are going to emit events to this
// then it must be made thread-safe like this instead
private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());
public void send(Object o) {
_bus.onNext(o);
}
public Observable<Object> toObserverable() {
return _bus;
}
public boolean hasObservers() {
return _bus.hasObservers();
}
}
在我的 Application Class
中,我这样做是为了初始化 BusClass
private ClientBus clientBus;
public ClientBus getRxBusSingleton() {
if (clientBus == null) {
clientBus = new ClientBus();
}
return clientBus;
}
在我想接收消息的 Activity 中,我注册了一个 CompositeSubscription
并从 Application Class
获取了对我的 ClientBus 类
的引用
clientBus = ((MyApplication) getApplicationContext()).getRxBusSingleton();
@Override
protected void onStart() {
super.onStart();
initSubscriptions();
}
@Override
protected void onStop() {
super.onStop();
_subscriptions.unsubscribe();
}
void initSubscriptions() {
_subscriptions = new CompositeSubscription();
_subscriptions.add(clientBus.toObserverable().subscribe(new Action1<Object>() {
@Override
public void call(Object event) {
Log.e("New Event", "Event Received");
if (event instanceof MyGcmListenerService.Message) {
String msg = ((MyGcmListenerService.Message) event).getMessage();
if (msg.equals("Update Available")) {
scheduleArrayList = getSchedules();
scheduleAdapter = new ScheduleAdapter(getApplicationContext(), scheduleArrayList, ScheduledUberActivity.this);
scheduledList.setAdapter(scheduleAdapter);
scheduleAdapter.notifyDataSetChanged();
} else if (msg.equals("Refresh")) {
fetchTrips();
}
}
}
}));
}
在 MyGcmListenerService 类
中,我在收到新通知时执行了此操作
private void sendRefreshNotif() {
if (clientBus.hasObservers()) {<--It enters the if cause the Log prints. But, the activity doesn't get the message
Log.e("Obervers", "Observers aren't null");
clientBus.send(new Message("Refresh"));
}
}
我不明白的是为什么它在这里不起作用?我用它在 Activity 和 fragment 之间进行交互。我关闭了我的应用程序以检查是否收到通知,它将进入此 block if (clientBus.hasObservers()) {
但它没有并启动应用程序并测试 Observer
,它注意到有一个活跃的观察者。有什么帮助吗?谢谢。
最佳答案
您似乎在 CompositeSubscription
和 MyApplication
中使用了 ClientBus
类的不同实例。尝试从 ClientBus
类创建一个单例,它对我来说很好。
public class ClientBus {
public ClientBus(SingletonAccessor accessor) {}
private static ClientBus instance;
private static class SingletonAccessor{}
public static ClientBus getInstance() {
if (instance == null) instance = new ClientBus(new SingletonAccessor());
return instance;
}
private final Subject<Object, Object> mBus = new SerializedSubject<>(PublishSubject.create());
public void send(Object o) {
mBus.onNext(o);
}
public Observable<Object> toObserverable() {
return mBus;
}
public boolean hasObservers() {
return mBus.hasObservers();
}
}
关于android - 配置 RxJava 以将数据从 GCMListenerService 发送到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943237/
我正在尝试使用 gcm 在 Android 设备上推送通知。到目前为止,我已经在 gcm 上注册并向我的后端发送了必要的信息,以便向构建 200 响应的 gcm 服务器发出发布请求。除了客户端接收消息
我已经按照本教程创建了一个新的 GCM 监听器服务: http://www.appifiedtech.net/2015/08/01/android-gcm-push-notification-exam
我正在使用 GCM。我想停止 GcmListenerService,这样我就可以停止应用程序接收不需要的通知。 我已经尝试了以下方法来关闭该服务,但它似乎并没有成功 Intent intent = n
在 AndroidManifest.xml 中我定义了以下组件
我的 GCM 服务有时会出现问题,当我的智能手机的 RAM 被自动清除时该服务会关闭(如果您需要更多详细信息,请阅读 this)。 据我所知,如果我将我的服务设置为在前台运行,它应该可以帮助系统使用
当应用程序处于后台或手机被锁定或处于 sleep 模式时,不会调用 GcmListenerService,但会触发通知。这将如何称呼当应用程序处于前台时,它的工作非常理想。GcmListenerSer
我正在尝试实现 GCM。一切正常,除了 onMessageReceived() 方法仅在应用程序运行时被调用。 我搜索了解决方案,但找不到与我相匹配的案例。我的 GCM 请求正文仅包含“to”和“da
我有以下 list - 并遵循 on
我正在尝试从服务器向我的应用程序获取消息。我有这个服务器 url,它向所有用户发送消息: https://taub-prayer-ramym.c9.io/send?message=123 我的应用程序
使用新的 GcmListenerService,我正在尝试在收到消息时获取用户位置: public class MobilAirGcmListenerService extends GcmListen
我目前正致力于在我的应用中实现 GCM 通知。 我遇到的问题是未调用我的 GcmListenerService 实现中的 onMessageReceived() 方法。我从 GCM 服务器收到数据很好
这是我的 list 文件
我点击了这个链接https://developers.google.com/cloud-messaging/android/start但我不明白一些事情。 我在哪里启动 GcmListenerServ
我正在使用 GCM 开发具有推送通知功能的 Android 应用程序。我创建了一个名为 PushNotificationService 的类,它扩展了 GCMListenerService。在 onM
Google 建议使用 GcmListenerService 代替 GcmBroadcastReceiver。 正在制作一个使用 GCM 的库项目(com.android.library 插件)。当应
当好友取消好友时,我正在使用 Otto 刷新好友列表。我在从非主线程更新 UI 时遇到问题,所以我调查了它并使用此“解决”了这个问题 post .他们使用的代码是这样的: public class B
我已按照说明设置 Android GCM 客户端应用程序 here ,并且对这段摘录有特别的疑问: For existing apps that extend a WakefulBroadcastRe
我正在尝试从我的 GCMServiceListener 向我的 Activity 发送更新,所以,我正在使用 RxJava/RxAndroid 并创建了一个 BusClass 用于处理发送和 Obse
自从我将 GCM 升级到 8.4.0 后,我的应用有时会崩溃并且日志显示以下异常: Fatal Exception: java.lang.RuntimeException: Unable to sta
我是一名优秀的程序员,十分优秀!