简体   繁体   中英

Firebase Permission denied crash

i have implemented firebase . but when child dont have permission app is crashing i am getting

Fatal Exception: com.google.firebase.database.d
 Firebase Database error: Permission denied
  Listener.onCancelled

and my inCancelled look like

@Override
public void onCancelled(DatabaseError databaseError) {
    try {
        Observable.onError(databaseError.toException());
    } catch (DatabaseException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }
}

but still it crash . how i can prevent this crash

i am saying if some one change child permission is any way we can stop this crash

This looks like problem with your google-services.json.

Make sure app name and client id are same as in firebase console.

If you are not sure re-download google-services.json from your project's console and add it your project.

If you attach a listener to a location where the user doesn't have read permission, the Firebase client will call onCancelled on that listener. Your current code raises that as an exception.

If you don't want to raise an exception, just handle onCancelled differently:

@Override
public void onCancelled(DatabaseError databaseError) {
    Toast.makeText(
        getApplicationContext(), 
        "You don't have permission to read this data", 
        Toast.LENGTH_SHORT).show();
}

An important thing to realize is that onCancelled being called is almost always a mistake in the code. It is not something the user of your app does wrong, but the code attaching a listener to the wrong location. So just displaying the error to the user is probably not the best idea.

For brevity you'll often see me raising the exception in sample snippets. But since this is a mistake in my code, in actual apps Id like this error to show up in Crash Reporting:

@Override
public void onCancelled(DatabaseError databaseError) {
    FirebaseCrash.report(databaseError.toException());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM