您好我是Rxjava的新手,并且使用Rxjava遇到FirebaseInstanceIdService问题。问题是,虽然已经调用onTokenReflesh,但它从不执行发送操作。 有人能解决这个问题吗? sendRegistrationToServer也被调用,我得到了刷新的令牌,但onCompleted,onError或onNext从未调用!!!
PS:由于某些原因,有时在服务终止之前调用onCompleted。所以我认为服务在ramdom死亡,无论可观察的完成情况如何。但那我该怎么处理呢?我需要这个observable来完成任务。 确切地说,当第一个活动被实例化时,我希望它发送由FirebaseInstanceId.getInstance()。getToken()生成的令牌。但是,它在那时返回null,因此我无法将其发送到服务器。几秒钟后,服务的onTokenReflesh被调用,所以我想这次发送tokenId。
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
CLog.d("onTokenRefresh");
// Get updated device token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(refreshedToken);
}
@Override
public void onDestroy() {
if (null != mSubscription){
mSubscription.unsubscribe();
}
super.onDestroy();
}
private void sendRegistrationToServer(String token){
CLog.d("token: " + token);
Observable<ApiResult> observable = ApiExecutor.makeClient(getApplicationContext())
.create(UUIDApi.class)
.createOrUpdateApplicationInfo(
uuid, token);
mSubscription =
ApiExecutor.extractAsyncObservable(observable, null)
.subscribe(new Observer<ApiResult>() {
@Override
public void onCompleted() {
CLog.d("onCompleted");
}
@Override
public void onError(Throwable e) {
CLog.d("onError" + e.getMessage());
}
@Override
public void onNext(ApiResult apiResult) {
CLog.d("onNext");
AppSettings.saveUUID(getApplicationContext(), uuid);
}
});
}
private Observable<ApiResult> extractAsyncObservable(Observable<ApiResult> observable){
return observable
.retry(3)
// when subscribed
.doOnSubscribe(() -> {
})
// when unSubscribed
.doOnUnsubscribe(() -> {
})
// called before onNext
.doOnNext(apiResult -> {
if (!apiResult.isSuccess()){
CLog.d("result_code: " + String.valueOf(apiResult.getResultCode()));
// TODO determine depending on a errorMsg
throw new RuntimeException(determineErrorMessage(apiResult.getResultCode()));
}
})
// called before onError
.doOnError(throwable -> {
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
;
}