Files
react-native-firebase/docs/crashlytics/quick-start.md
2020-01-31 11:58:33 +00:00

4.1 KiB

title, description
title description
Quick Start Getting started with Crashlytics in React Native Firebase

Crashlytics Quick Start

Installation

This module depends on the @react-native-firebase/app module. To get started and install app, visit the project's quick start guide.

Install this module with Yarn:

yarn add @react-native-firebase/crashlytics

Integrating manually and not via React Native auto-linking? Check the setup instructions for Android & iOS.

Both platforms require additional steps to complete installation:

Follow the Android steps to complete Crashlytics integration.

Module usage

The Crashlytics package will automatically report on any fatal application crash. Both native and JavaScript crashes along with unhandled promise rejections are captured with full stack traces.

The package provides a JavaScript API to create your own crash reports and/or send additional information with crash reports to the Firebase console for a better debugging experience.

Once installed, import the Crashlytics package into your project:

import crashlytics from '@react-native-firebase/crashlytics';

You need to import the library even if you're not using any functions of the Crashlytics SDK. By doing this, you enable the listener that get the JS stacktrace into the Crash Reporting console in Firebase

Consider putting this in index.js file.

import '@react-native-firebase/crashlytics';

The package also provides access to the firebase instance:

import { firebase } from '@react-native-firebase/crashlytics';

Testing crashes

The Crashlytics package provides a crash method which is provided to ensure crash reports are correctly being sent to the Firebase console and can be used with your own test suite. Any log methods called before sending a crash report and sent with the crash report.

import crashlytics from '@react-native-firebase/crashlytics';

function forceCrash() {
  crashlytics().log('Testing crash');
  crashlytics().crash();
}

Handling non-fatal crashes

Crashlytics supports sending JavaScript stack traces to the Firebase console. This can be used in any situations where an error occurs but is caught by your own code as a bailout method. To send a stack trace, pass a JavaScript Error to the recordError method.

import crashlytics from '@react-native-firebase/crashlytics';

function sendReport() {
  crashlytics().recordError(new Error('Error with a stack trace'));
}

Additional crash attributes

It is possible to provide additional attributes which are sent with crash reports. This helps with debugging and localising the error. To set additional attributes call the set* methods.

import crashlytics from '@react-native-firebase/crashlytics';

async function onSignIn(user) {
  await Promise.all([
    crashlytics().setUserId(user.uid),
    crashlytics().setUserName(user.username),
    crashlytics().setUserEmail(user.email),
    crashlytics().setAttribute('credits', user.credits),
  ]);

  crashlytics.crash();
}

Disabling Crashlytics automatic collection

To disable Crashlytics so you can manually opt-in your app users, set the crashlytics_auto_collection_enabled option to false in the firebase.json file.

Once your user has consented, enable Crashlytics collection via the JavaScript API:

await firebase.crashlytics().setCrashlyticsCollectionEnabled(true);

Enabling Debug Reports

By default, Crashlytics won't report crashes and logs from builds in debug mode. To enable it, set the crashlytics_debug_enabled option to true in the react-native section of your firebase.json config file.

To learn more about all the firebase.json options, view the documentation here.