mFirebaseRemoteConfig.fetch() doesn't return - android

mFirebaseRemoteConfig.fetch(0)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
System.out.println("Fetch Succeeded");
// Once the config is successfully fetched it must be
// values are returned.
mFirebaseRemoteConfig.activateFetched();
} else {
System.out.println("Fetch failed");
}
}
});
I added this to get the remote config from the server. I was able to get the values a couple of times. I updated the remote config conditions after that and now fetch doesnt return anything. Tried a lot of approaches including moving the call after on onResume and calling it from a separate thread. Updating to 9.2.1 also didnt worked for me
What else can be done to get the config?

Related

addOnFailureListener not working in offline mode and network issues

addOnFailureListner does not work for add() data, but addOnFailureListner works for get().
This is not working
WorkPlaceRef.add(DATA).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
#Override
public void onComplete(#NonNull Task<DocumentReference> task) {
//Successfully created - This one triggers when I turn on wifi again.
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Error - This addonFailureListner is not working when there are no network.
}
});
This is working
WorkPlaceRef.get().addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
#Override
public void onComplete(#NonNull Task<DocumentReference> task) {
//Successfully received - This one triggers when I turn on wifi again.
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Error - this addonFialureListner triggers when there is no network.
}
});
It's not a failure to attempt to write data while offline. The Firestore SDK will write the data in a local offline cache first, and eventually synchronize that write with the server when the app comes back online. The success listener will be invoked whenever that happens.
Write failures only happen when there is some problem that can't be retried due to lack of connectivity, such as security rule violations, or exceeding some documented limit of the database.
If you want to know if some document data is not yet synchronized with the server, you can check its metadata to know if a write is pending.

How can I subscribe user to a topic for notifications as soon as app starts up the android app?

How can I subscribe user to a topic for notifications as soon as app starts up?
I have an app I want to subscribe all those that they will install the app automatically on firebase messaging so that i can send them notification on ANDROID STUDIO
Per the Firebase Messaging documentation, you can simply call subscribeToTopic in your main activity's onCreate. To do this, you can add this line:
FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name");
If you would like to take some action after subscribing, you can add a listener that will get called when the subscription action is complete. For example (in Java):
FirebaseMessaging.getInstance().subscribeToTopic("your_topic_name")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
// do something, such as showing a Toast to the user
} else {
// subscribing failed for some reason, maybe perform some error-handling
}
}
});
There are more examples in the documentation above, including a Kotlin example (you didn't specify which language you were using, so I made an assumption), which you may find useful. Hope that helps!
FirebaseMessaging.getInstance().subscribeToTopic("your_topic")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
for more information read the documentation

Cloud Firestore: retrieving cached data via direct get?

Retrieving data from the server may take some seconds. Is there any way to retrieve cached data in the meantime, using a direct get?
The onComplete seems to be called only when the data is retrieved from the server:
db.collection("cities").whereEqualTo("state", "CA").get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
...
}
}
});
Is there any callback for the cached data?
Now it is possible to load data only from cached version. From docs
You can specify the source option in a get() call to change the default behavior.....you can fetch from only the offline cache.
If it fails, then you can again try for the online version.
Example:
DocumentReference docRef = db.collection("cities").document("SF");
// Source can be CACHE, SERVER, or DEFAULT.
Source source = Source.CACHE;
// Get the document, forcing the SDK to use the offline cache
docRef.get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
// Document found in the offline cache
DocumentSnapshot document = task.getResult();
Log.d(TAG, "Cached document data: " + document.getData());
} else {
Log.d(TAG, "Cached get failed: ", task.getException());
//try again with online version
}
}
});
I just ran a few tests in an Android app to see how this works.
The code you need is the same, no matter if you're getting data from the cache or from the network:
db.collection("translations").document("rPpciqsXjAzjpComjd5j").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot snapshot = task.getResult();
System.out.println("isFromCache: "+snapshot.getMetadata().isFromCache());
}
});
When I'm online this prints:
isFromCache: false
When I go offline, it prints:
isFromCache: true
There is no way to force retrieval from the cache while you're connected to the server.
If instead I use a listener:
db.collection("translations").document("rPpciqsXjAzjpComjd5j").addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException e) {
System.out.println("listen.isFromCache: "+snapshot.getMetadata().isFromCache());
}
}
);
I get two prints when I'm online:
isFromCache: true
isFromCache: false
You can disable network access and run the query to access data from cache .
For firestore :
https://firebase.google.com/docs/firestore/manage-data/enable-offline#disable_and_enable_network_access
For firebase database call db.goOffline() and db.goOnline()

Remote Config doesnt fetch anything

I'm having an issue with Firebase's remote config, the same code works just fine in another app, but on this app i'm working on i'm barely works!! i've checked "connect your app to firebase" and "add remote config to your app" everything is OK, so i think the problem is somehow with the code, because i don't see any "fetch succeded" or "fetch failed" in logs:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mRemoteConfig.fetch(0)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
Log.d("firebaseconfig", "Fetch Succeeded");
// Once the config is successfully fetched it must be activated before newly fetched
// values are returned.
mRemoteConfig.activateFetched();
} else {
Log.d("firebaseconfig", "Fetch failed");
}
}
});
}
}, 0);
Any help pleeease? Thank you very much

Firebase Database Cashed data

Is there any way to know data which is not uploaded on firebase and is persisting on disk? I have enabled offline capability by using-
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
By doing this my all data remains cached. I want to know which data is uploaded and which isnt.
There is no method in the API that returns an indication of which write operations have not yet been committed to the Firebase server. If you need that status, you can manage it yourself. The setValue() and updateChildren() methods provide two options for getting a callback when the value is committed to the Firebase server. One option is to provide a CompletionListener parameter:
FirebaseDatabase.getInstance().getReference("test")
.setValue("SomeValue", new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
// the data was successfully committed to the server
} else {
// the operation failed, for example permission failure
}
}
});
The other option is to add a listener to the returned Task:
FirebaseDatabase.getInstance().getReference("test")
.setValue("SomeValue").addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
// the data was successfully committed to the server
} else {
// the operation failed, for example permission failure
}
}
});
For both cases, the update is in the local cache immediately after the call to setValue() or updateChildren(), but is not in the server database until the callback fires.

Categories

Resources