belen
belen

Reputation: 193

Show dialog to ask the user to report a crash after it happened with Firebase Crashlytics

I am using Crashlytics from Firebase, the crash reports are been uploaded fine to the Crashlytics console. Now my intention is upload a crash report only if the user consent to do so. For that I want to show the report to the user and ask if he/she wants to send it. I want to do that with every crash that occurs. Is this possible?

So far I only found in the documentation how to Enable opt-in reporting

By default, Firebase Crashlytics automatically collects crash reports for all your app's users. To give users more control over the data they send, you can enable opt-in reporting instead.

To do that, you have to disable automatic collection and initialize Crashlytics only for opt-in users.

  1. Turn off automatic collection with a meta-data tag in your AndroidManifest.xml file:
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="false" />

Enable collection for selected users by initializing Crashlytics from one of your app's activities:

Fabric.with(this, new Crashlytics());

But I would like to be able to capture the crash when it happens, and when the app is been started again show the dialog with the information and asking to the user for the consent to upload it to Crashlytics console.

Upvotes: 4

Views: 983

Answers (1)

Gaket
Gaket

Reputation: 6829

You can create a custom UncaughtExceptionHandler.

  1. In your Application class you init the Crashlytics as usual.
  2. Then set default exceptions handler to your custom one:

    UncaughtExceptionHandler customHandler = 
        new UserAgreementExceptionHandler(Thread.getDefaultUncaughtExceptionHandler());
    Thread.setDefaultUncaughtExceptionHandler(customHandler);
    

In this case, all the uncaught exceptions will be passed to your handler. Next goal is to tailor it for your needs. It will look like this:

public class UserAgreementExceptionHandler implements Thread.UncaughtExceptionHandler {

  @NonNull
  private Thread.UncaughtExceptionHandler defaultHandler;

  public UserAgreementExceptionHandler(@NonNull Thread.UncaughtExceptionHandler defaultHandler) {
    this.defaultHandler = defaultHandler;
  }

  @Override
  public void uncaughtException(Thread thread, Throwable ex) {
    // Show dialog to user, pass callbacks for answers inside

    // If user agreed to send the data, 
    // call the default handler you passed on object creation:

    // defaultHandler.uncaughtException(thread, ex);

    // Otherwise, don't pass it further
  }
}

The only question is how to start a dialog from it and it depends from your app architecture. You can inject some business logic class inside that will wait to show a dialog using the Activity Context that is on screen now.

Upvotes: 1

Related Questions