Firebase Realtime database - Android activity crashes after storing data - android

I'm trying to store an object Reservation into Firebase Realtime Db but after the storing method is called, the activity crashes and there is no exception being thrown.
private void makeReservation(Reservation reservation)
{
DatabaseReference dbRef = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.child(serviceProviderId);
// Create a reservationMap containing the reservation properties
HashMap<String, Object> reservationMap = new HashMap<>();
reservationMap.put("date", reservation.getDate());
reservationMap.put("time", reservation.getTime());
reservationMap.put("username",reservation.getUserName());
reservationMap.put("service_provider_name",reservation.getServiceProviderName());
reservationMap.put("category",reservation.getCategoryName());
reservationMap.put("subcategory",reservation.getSubcategoryName());
reservationMap.put("selected_date",reservation.getSelectedDate());
// Create a new child node with a unique ID under the "reservations" node
DatabaseReference reservationsRef = dbRef.child("reservations").push();
// Set the value of the child node to the reservationMap
reservationsRef.setValue(reservationMap).addOnSuccessListener(aVoid -> {
// Reservation saved successfully
Toast.makeText(getApplicationContext(),"Reservation saved successfully",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ChosenUser.this, MenuActivity.class);
startActivity(intent);
finish();
}).addOnFailureListener(e -> {
// Reservation saving failed
Toast.makeText(getApplicationContext(),"Error saving reservation: " + e.getMessage(),Toast.LENGTH_SHORT).show();
});
}
try {
// Code to save the reservation to Firebase
makeReservationWithAuth(reservation);
} catch (Exception e) {
// Handle the exception, for example, by logging an error message
Toast.makeText(getApplicationContext(),"Error saving reservation: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
I checked the logcat and saw this is only error :
java.lang.SecurityException: Specified package android under uid 10333 but it is really 1000
So I though that the issue might be due to fact that the storing method doesn't check if user is logged in, so I updated the makeReservation() to :
private void makeReservationWithAuth(Reservation reservation)
{
FirebaseAuth mAuth = FirebaseAuth.getInstance();
// Get a reference to the current user
FirebaseUser currentUser = mAuth.getCurrentUser();
// Check if the user is authenticated
if (currentUser != null) {
// User is authenticated, so we can save the reservation
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference().child("users").child(currentUser.getUid());
DatabaseReference reservationsRef = userRef.child("reservations").push();
HashMap<String, Object> reservationMap = new HashMap<>();
reservationMap.put("reservationID", reservationsRef.getKey());
reservationMap.put("date", reservation.getDate());
reservationMap.put("time", reservation.getTime());
reservationsRef.setValue(reservationMap);
} else {
// User is not authenticated, so we cannot save the reservation
Toast.makeText(this, "You must be logged in to make a reservation", Toast.LENGTH_SHORT).show();
}
}
But the issue still happens, after the method is called, the activity crashes.
I tried debbuging line by line until reservationsRef.setValue(reservationMap) and no exception was thrown, after this line the app crashes.
What could be wrong ?

Related

Unable to add data to document in firestore (Android)

I'm trying to add data to a document to Firebase Firestore. I've added a collection named users to it. Also the read/write permissions are open for now. I'm following this doc. And I'm not able to add data to document.
Here is what I'm trying to do:
private void getNewUserSnapShot() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
Log.d(TAG, "getNewUserSnapShot: user_uid: " + user.getUid());
DocumentReference user_doc_ref = db.collection("users").document();
Log.d(TAG, "getNewUserSnapShot: document ref: " + user_doc_ref.getId());
Map<String, Object> user_data = new HashMap<>();
user_data.put("name", user.getDisplayName());
user_data.put("email", user.getEmail());
user_data.put("profile_url", user.getPhotoUrl());
Log.d(TAG, "getNewUserSnapShot: user_data: " + user_data.toString());
user_doc_ref
.set(user_data)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(LoginActivity.this, task.toString(), Toast.LENGTH_LONG).show();
if (task.isSuccessful()) {
Log.d(TAG, "getNewUserSnapShot: success");
} else {
Log.d(TAG, "getNewUserSnapShot: failed");
}
}
});
}
In Logs I see only these (neither the log for failure nor for success):
2020-03-04 19:48:47.489 30744-30744/com.example.expenditure D/LoginActivity: getNewUserSnapShot: user_uid: iXOzfju6kORnhuUND8zFCPTzxY93
2020-03-04 19:48:47.499 30744-30744/com.example.expenditure D/LoginActivity: getNewUserSnapShot: document ref: 7AluPzcYMLzDKLh8YtBt
2020-03-04 19:48:47.499 30744-30744/com.example.expenditure D/LoginActivity: getNewUserSnapShot: user_data: {profile_url=https://someurl/security/reasons, name=Nikhil Wagh, email=null}
And when I see firebase console, I can't find the document with ID 7AluPzcYMLzDKLh8YtBt, according to logs which should have been created.
There is a similar question: Unable to add information to Cloud Firestore But it doesn't have right answers.
Can someone help. What am I doing wrong?
The issue was user.getPhotoUrl() returns url, but Firestore doesn't support urls. The url needs to be casted as a string, and then it works.
Cast your urls to string before adding data to Firestore.

cannot delete firestore reference in batch

I am trying to move firestore user records after authentication using batch. It seems to copy the records but not deleting.
Android app begins in anonymous mode, when user decides to signup, i try to move his records to the new SMS authenticated user.
Before signup, i get all user records using listRef.get() and a Success listener. I than save the resulted QuerySnapshot. After SMS authentication success, i use the snapshot to copy and delete.
I use the snapshot.getReference() to find the record. I confirmed in debug mode the reference path is correct
To make sure the delete code working, i made a test method and it worked.
The path in firestore is: lists/users/uid/
private void retrieveDataBeforeVerify() {
CollectionReference listRef = db.collection( "lists" ).document("users").
collection( mCurrentUser.getUid() );
listRef.get()
.addOnSuccessListener( new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
mQuerySnapshot = queryDocumentSnapshots; //Save snapshort
startPhoneNumberVerification(mPhoneNumberField.getText().toString()); //Start auth
}
} );
}
}
After the SMS authentications
private void moveDataToNewUser() {
if (mQuerySnapshot != null) {
if (mCurrentUser != null) {
DocumentReference listRef = db.collection( "lists" ).document( "users" );
WriteBatch batch = db.batch();
for (QueryDocumentSnapshot documentSnapshot : mQuerySnapshot) {
Main main = documentSnapshot.toObject( Main.class );
batch.set( listRef.collection( mCurrentUser.getUid() ).document( ) , main); //Copy working correctly
DocumentReference reference = documentSnapshot.getReference(); //On debug, reference shows correct path
batch.delete( reference ); //Delete does not work
}
batch.commit().addOnFailureListener( new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e( TAG, "moveDataToNewUser: batch failed" );
}
} ).addOnSuccessListener( new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d( TAG, "moveDataToNewUser: batch success" );
}
} );
}
}
}
update
I changed the reference to something non existing and still the onSuccess listener fires. How can it be??
DocumentReference reference = documentSnapshot.getReference().collection( "22" ).document("123");
update
if i comment the first two lines on the for-loop then delete is success. It looks as if the batch cannot process both set and delete on the same batch. I also tried using a transaction but got similar results. does this makes sense?
btw - i am using com.google.firebase:firebase-firestore:18.2.0, i believe it is the latest
for (QueryDocumentSnapshot documentSnapshot : mQuerySnapshot) {
//Main main = documentSnapshot.toObject( Main.class );
//batch.set( listRef.collection( mCurrentUser.getUid() ).document( ) , main); //Copy working correctly
DocumentReference reference = documentSnapshot.getReference(); //On debug, reference shows correct path
batch.delete( reference ); //Delete success!
}

Getting documents from Firestore after network connection gets reset - Android

I am using Cloud Firestore in my Android app. It's a quiz application where I randomly get documents from Firestore. When the internet connection is good, the app works fine. When the network gets disconnected and then again gets connected, I am unable to read the documents. When I debug, I find that my get() method is not getting executed at all.
Iterator iterator = randomIds.iterator();
while (iterator.hasNext()) {
String documentId = (String) iterator.next();
DocumentReference documentReference = db.collection(categoryName).document(documentId);
if (documentReference!=null) {
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
System.out.println("Task successful");
DocumentSnapshot document = task.getResult();
questionDetailsObj = new QuestionDetails();
questionDetailsObj = document.toObject(QuestionDetails.class);
if (questionDetailsObj != null) {
System.out.println("Question details: " + questionDetailsObj.getQuestion_text());
// Adding the questions to a list
questionsList.add(questionDetailsObj);
}
} else {
Log.d("MainActivity", "get() failed with " + task.getException());
}
}
});
}
}
I want to retrieve 10 documents. Sometimes, few documents are retrieved successfully and for the others I get the exception
get() failed with com.google.firebase.firestore.FirebaseFirestoreException:
Failed to get document because the client is offline.
I don't understand why would some documents come successfully and some fail to get retrieved. Please help me understand if any code changes are required.

Firebase suddenly stopped working, using Anonymous Auth

i have set up firebase storage for my app, and added the code for anonymous auth on the app and on the firebase console.
it worked at first, but i dont know why it stopped working, saying that the user does not have permission to access the object
Anonymous auth is correctly set up and i did see it working, code is almost like Google Firebase docs
logcat:
D/FirebaseAuth: signInAnonymously:onComplete:true
D/FirebaseAuth:
onAuthStateChanged:signed_in: (Random auth user id)
... When i request the item from firebase
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.FirebaseException: An internal error has occured. [Internal error encountered.]
I/DpmTcmClient: RegisterTcmMonitor
from: com.android.okhttp.TcmIdleTimerMonitor W/NetworkRequest: no auth
token for request E/StorageException: StorageException has occurred.
User does not have permission to access this object.
Code: -13021 HttpResult: 403
Can Someone help?
Declaring Variables
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
on the OnCreate method
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
}
// ...
}
};
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d("FirebaseAuth", "signInAnonymously:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("FirebaseAuth", "signInAnonymously", task.getException());
Toast.makeText(SingleMemeEditor.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
and the method that gets from storage:
Bitmap bmp;
final Context lContext = context; //getting the MainActivity Context
final String lFileName = fileName; //filename to download
final String lCatPath = catPath; //internal categorization folder
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl(context.getResources().getString(R.string.firebase_bucket));
// Create a reference with an initial file path and name
StorageReference filesRef = storageRef.child("files/" + fileName);
try
{
final File localFile = File.createTempFile("images", "jpg");
filesRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>()
{
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot)
{
// Local temp file has been created
File file = new File(getDirectory(lContext)
+ File.separator + lCatPath + File.separator + lFileName);
try
{
Boolean b = file.createNewFile();
if(b)
{
FileInputStream in = new FileInputStream(localFile);
FileOutputStream out = new FileOutputStream(file);
// Transfer bytes from in to out
byte[] buf = new byte[(int)localFile.length()];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
Drawable.createFromPath(file.getPath())).getBitmap());
}
catch (IOException ex)
{
// Handle any errors
Log.e("CopyingFromTemp", ex.getMessage());
}
}
}).addOnFailureListener(new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception ex)
{
// Handle any errors
Log.e("FirebaseDownloadError", ex.getMessage());
}
});
}
catch(Exception ex)
{
Log.e("FirebaseDownloadError", ex.getMessage());
}
also i'm using standard security rules:
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
as Benjamin Wulfe hinted, i deleted the App's data on the phone and it worked, which means that some kind of Token data was stored on the phone and Anonymous Auth was getting an old session data.
so i added a sign out code before signInAnonymously
mAuth.signOut();
and done!
Thanks to you all for the help!
EDIT: I found another method which is better than signing out and in again (which lead to hundreds of unused anonymous users on the firebase console, and that because the app is not in production yet, would have been millions).
this is what i did:
if (mAuth.getCurrentUser() != null)
{
mAuth.getCurrentUser().reload();
}
else
{
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
#Override
public void onComplete(#NonNull Task<AuthResult> task)
{
Log.d("FirebaseAuth", "signInAnonymously:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful())
{
Log.w("FirebaseAuth", "signInAnonymously", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
this just reloads current authenticated (anonymous) user.
The message " W/NetworkRequest: no auth token for request " is key for debugging this issue.
This log message means that Firebase Storage did not see any login in the current context. This includes anonymous logins. It means that no authorization was passed to the backend and the only way this will be allowed is if you set your rules to be completely open (public access) which is not recommended (see below).
//this sets completely open access to your data
allow read, write;
I would review the code you have for logging in and ensure it successfully completes before any storage operation is done.
If you are sure your auth code is correct, try resetting data on the device so that no saved state might be there to mess up the application's authorization information.
You might have to check your RULES for storage in firebase console. By default it is set to only permit to authenticated user only
like this
allow read, write: if request.auth != null;
Sometime its disconnect from firebase database,So Connect your app with firebase authentication in android studio through firebase assistance tool.

Android: How to input values to Parse cloud code, eg beforesave

Inside a app, users will upload slot results with period name to the Parse Database. However, before upload, it would be much preferred if beforesave, checked whether the period ref is already there, if the same period ref is existing in the DB, the slot result would not be uploaded.
Cloud.beforesave
Parse.Cloud.beforeSave("check_duplicate", function(request, response)
{
var DB = Parse.Object.extend("Record_db");
var query = new Parse.Query(DB);
query.equalTo("period_ref", request.object.get("period_ref"));
query.first
({
success: function(object)
{
if (object)
{
response.error("A Period with this ref already exists.");
}
else
{
response.success();
}
},
error: function(error)
{
response.error("Could not validate uniqueness for this period ref object.");
}
});
});
Android code:
ParseCloud.callFunctionInBackground("check_duplicate", new HashMap<String, Object>(), new FunctionCallback<String>() {
public void done(String result, ParseException e)
{
if (e == null)
{
Utilities.custom_toast(CurrentResult.this, "cloud success" + result, "gone!", "short");
}
else
{
Utilities.custom_toast(CurrentResult.this, "cloud error" + e, "gone!", "short");
}
}
});
Question:
There is no clear example for such common situation. I would like to ask
for example, now the user would like to upload slot ref 001/2015 results. All info are already available at device, how could I pass this period reference 001/2015 to the cloud code for checking whether it is already existing in the Cloud DB uploading and saving to the Cloud DB?
Thanks a lot!
your first line of Android...
ParseCloud.callFunctionInBackground("check_duplicate", new HashMap(), new FunctionCallback() {
becomes
ParseCloud.callFunctionInBackground("check_duplicate",
new HashMap<String, String>{"period_ref":"001/2015"};,
new FunctionCallback<String>() {

Categories

Resources