Add Firebase Crash Reporting for iOS and Android

This commit is contained in:
Chris Bianca 2017-03-10 14:17:37 +00:00
parent d86a3dff2a
commit 77a09f5810
7 changed files with 132 additions and 1 deletions

View File

@ -52,5 +52,5 @@ dependencies {
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.google.firebase:firebase-storage:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.google.firebase:firebase-crash:10.2.0'
}

View File

@ -17,6 +17,7 @@ import io.invertase.firebase.auth.RNFirebaseAuth;
import io.invertase.firebase.storage.RNFirebaseStorage;
import io.invertase.firebase.database.RNFirebaseDatabase;
import io.invertase.firebase.analytics.RNFirebaseAnalytics;
import io.invertase.firebase.crash.RNFirebaseCrash;
import io.invertase.firebase.messaging.RNFirebaseMessaging;
@SuppressWarnings("unused")
@ -39,6 +40,7 @@ public class RNFirebasePackage implements ReactPackage {
modules.add(new RNFirebaseAnalytics(reactContext));
modules.add(new RNFirebaseStorage(reactContext));
modules.add(new RNFirebaseMessaging(reactContext));
modules.add(new RNFirebaseCrash(reactContext));
return modules;
}

View File

@ -0,0 +1,40 @@
package io.invertase.firebase.crash;
import android.util.Log;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.google.firebase.crash.FirebaseCrash;
public class RNFirebaseCrash extends ReactContextBaseJavaModule {
private static final String TAG = "RNFirebaseCrash";
public RNFirebaseCrash(ReactApplicationContext reactContext) {
super(reactContext);
Log.d(TAG, "New instance");
}
@Override
public String getName() {
return TAG;
}
@ReactMethod
public void log(final String message) {
FirebaseCrash.log(message);
}
@ReactMethod
public void logcat(final int level, final String tag, final String message) {
FirebaseCrash.logcat(level, tag, message);
}
@ReactMethod
public void report(String message) {
FirebaseCrash.report(new Exception(message));
}
}

View File

@ -0,0 +1,12 @@
#ifndef RNFirebaseCrash_h
#define RNFirebaseCrash_h
#import "RCTBridgeModule.h"
@interface RNFirebaseCrash : NSObject <RCTBridgeModule> {
}
@end
#endif

View File

@ -0,0 +1,26 @@
#import "RNFirebaseCrash.h"
#import "Firebase.h"
@implementation RNFirebaseCrash
RCT_EXPORT_MODULE(RNFirebaseCrash);
RCT_EXPORT_METHOD(log:(NSString *)message)
{
FIRCrashLog(message);
}
RCT_EXPORT_METHOD(logcat:(nonnull NSNumber *) level
tag:(NSString *) tag
message:(NSString *) message)
{
FIRCrashLog(message);
}
RCT_EXPORT_METHOD(report:(NSString *) message)
{
FIRCrashLog(message);
assert(NO);
}
@end

View File

@ -14,6 +14,7 @@ import Storage from './modules/storage';
import Database from './modules/database';
import Messaging from './modules/messaging';
import Analytics from './modules/analytics';
import Crash from './modules/crash';
let log;
const instances = { default: null };
@ -67,6 +68,7 @@ export default class Firebase extends Singleton {
_constants: ?Object;
_messaging: ?Object;
_remoteConfig: ?Object;
_crash: ?Object;
/**
* Support web version of initApp.
@ -148,6 +150,13 @@ export default class Firebase extends Singleton {
return this._messaging;
}
crash() {
if (!this._crash) {
this._crash = new Crash(this);
}
return this._crash;
}
get apps(): Array<string> {
return Object.keys(instances);
}

View File

@ -0,0 +1,42 @@
// @flow
import { NativeModules } from 'react-native';
import { Base } from './../base';
const FirebaseCrash = NativeModules.RNFirebaseCrash;
export default class Analytics extends Base {
/**
* Logs a message that will appear in a subsequent crash report.
* @param {string} message
* @param params
*/
log(message: string): void {
FirebaseCrash.log(message);
}
/**
* Logs a message that will appear in a subsequent crash report as well as in logcat.
* NOTE: Android only functionality. iOS will just log the message.
* @param {string} message
* @param {number} level
* @param {string} tag
*/
logcat(level: number, tag: string, message: string): void {
FirebaseCrash.logcat(level, tag, message);
}
/**
* Generates a crash report for the given message. This method should be used for unexpected
* exceptions where recovery is not possible.
* NOTE: on iOS, this will cause the app to crash as it's the only way to ensure the exception
* gets sent to Firebase. Otherwise it just gets lost as a log message.
* @param {string} message
*/
report(message: string): void {
FirebaseCrash.report(message);
}
get namespace(): string {
return 'firebase:crash';
}
}