Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 715862d

Browse files
Added Log with Crashlytics #967
1 parent 8ce8bd4 commit 715862d

File tree

15 files changed

+204
-197
lines changed

15 files changed

+204
-197
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ src/platforms/android/include.gradle
1616
!src/index.d.ts
1717
!src/references.d.ts
1818
!src/analytics/analytics.d.ts
19+
!src/crashlytics/crashlytics.d.ts
1920
!src/storage/storage.d.ts
2021
!src/admob/admob.d.ts
2122
!src/messaging/messaging.d.ts

demo/app/main-view-model.ts

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { Observable } from "tns-core-modules/data/observable";
22
import { alert, prompt } from "tns-core-modules/ui/dialogs";
3-
import { isIOS, isAndroid } from "tns-core-modules/platform";
3+
import { isAndroid, isIOS } from "tns-core-modules/platform";
44
import * as firebase from "nativescript-plugin-firebase";
5-
import { AddEventListenerResult, storage as firebaseStorage, admob as firebaseAdMob, User } from "nativescript-plugin-firebase";
5+
import {
6+
AddEventListenerResult,
7+
admob as firebaseAdMob,
8+
crashlytics as firebaseCrashlytics,
9+
storage as firebaseStorage,
10+
User
11+
} from "nativescript-plugin-firebase";
612
import * as fs from "tns-core-modules/file-system";
713
import { MessagingViewModel } from './messaging-view-model';
814

@@ -1626,47 +1632,26 @@ export class HelloWorldModel extends Observable {
16261632
public doLogMessageCrashlytics(): void {
16271633
if (isAndroid) {
16281634
// Send the desired exception
1629-
firebase.sendCrashlyticsLog(new java.lang.Exception("test Exception")).then(
1630-
() => {
1631-
alert({
1632-
title: "Message logged",
1633-
message: "Check the Firebase console",
1634-
okButtonText: "Okay"
1635-
});
1636-
},
1637-
error => {
1638-
alert({
1639-
title: "Logging error",
1640-
message: error,
1641-
okButtonText: "OK"
1642-
});
1643-
}
1644-
);
1635+
firebaseCrashlytics.sendCrashLog(new java.lang.Exception("test Exception"));
16451636
} else if (isIOS) {
16461637
// Send the desired exception
1647-
firebase.sendCrashlyticsLog(new NSError({domain: 'ShiploopHttpResponseErrorDomain', code: 42, userInfo: null})).then(
1648-
() => {
1649-
alert({
1650-
title: "Message logged",
1651-
message: "Check the Firebase console",
1652-
okButtonText: "Okay"
1653-
});
1654-
},
1655-
error => {
1656-
alert({
1657-
title: "Logging error",
1658-
message: error,
1659-
okButtonText: "OK"
1660-
});
1661-
}
1662-
);
1638+
firebaseCrashlytics.sendCrashLog(new NSError({
1639+
domain: 'ShiploopHttpResponseErrorDomain',
1640+
code: 42,
1641+
userInfo: null
1642+
}));
16631643
}
1664-
1665-
}
16661644

1645+
alert({
1646+
title: "Message logged",
1647+
message: "Check the Firebase console",
1648+
okButtonText: "Okay"
1649+
});
1650+
}
16671651

16681652
public doSetCrashlyticString(): void {
1669-
firebase.setCrashlyticsString("test_key", "test_value");
1653+
firebaseCrashlytics.setString("test_key", "test_value");
1654+
16701655
alert({
16711656
title: "String created",
16721657
message: "New string key created, log a new message and check firebase console",
@@ -1675,7 +1660,8 @@ export class HelloWorldModel extends Observable {
16751660
}
16761661

16771662
public doSetCrashlyticBool(): void {
1678-
firebase.setCrashlyticsBool("test_key_bool", true);
1663+
firebaseCrashlytics.setBool("test_key_bool", true);
1664+
16791665
alert({
16801666
title: "Bool created",
16811667
message: "New string key created, log a new message and check firebase console",
@@ -1684,7 +1670,8 @@ export class HelloWorldModel extends Observable {
16841670
}
16851671

16861672
public doSetCrashlyticInt(): void {
1687-
firebase.setCrashlyticsInt("test_key_int", 2);
1673+
firebaseCrashlytics.setInt("test_key_int", 2);
1674+
16881675
alert({
16891676
title: "Int created",
16901677
message: "New string key created, log a new message and check firebase console",
@@ -1693,7 +1680,8 @@ export class HelloWorldModel extends Observable {
16931680
}
16941681

16951682
public doSetCrashlyticDouble(): void {
1696-
firebase.setCrashlyticsDouble("test_key_double", 56615.55548465);
1683+
firebaseCrashlytics.setDouble("test_key_double", 56615.55548465);
1684+
16971685
alert({
16981686
title: "Double created",
16991687
message: "New string key created, log a new message and check firebase console",
@@ -1702,7 +1690,8 @@ export class HelloWorldModel extends Observable {
17021690
}
17031691

17041692
public doSetCrashlyticFloat(): void {
1705-
firebase.setCrashlyticsFloat("test_key", 54646.45);
1693+
firebaseCrashlytics.setFloat("test_key", 54646.45);
1694+
17061695
alert({
17071696
title: "Float created",
17081697
message: "New string key created, log a new message and check firebase console",
@@ -1711,7 +1700,9 @@ export class HelloWorldModel extends Observable {
17111700
}
17121701

17131702
public doSetUserId(): void {
1714-
firebase.setUserIdCrashlytics("user#42");
1703+
// just for fun: showing usage of 'firebase.crashlytics' instead of 'firebaseCrashlytics'
1704+
firebase.crashlytics.setUserId("user#42");
1705+
17151706
alert({
17161707
title: "User id changed",
17171708
message: "Log a new message and check firebase console",

demo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"firebase-functions": "^2.0.5",
13-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.1.6.tgz",
13+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.2.0.tgz",
1414
"nativescript-theme-core": "^1.0.4",
1515
"nativescript-unit-test-runner": "^0.3.4",
1616
"tns-core-modules": "^4.2.1"
@@ -37,4 +37,4 @@
3737
"build.plugin": "cd ../src && npm run build",
3838
"ci.tslint": "npm i && tslint --config '../tslint.json' 'app/**/*.ts' --exclude '**/node_modules/**' --exclude '**/typings/**'"
3939
}
40-
}
40+
}

docs/CRASHREPORTING.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
1-
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/crash.png" height="85px" alt="Crash Reporting"/>
1+
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/crashlytics.png" height="85px" alt="Crashlytics"/>
2+
3+
> Firebase Crash reporting is deprecated - use Crashlytics if you can!
24
35
## Enabling Crash Reporting / Crashlytics
46
Since version 5.3.0 you can choose either Crashlytics (recommended) or the older Firebase Crash Reporting (which doesn't really work on iOS).
57
You will be prompted during installation of the plugin to enable either Crashlytics or Crash Reporting (both are off by default).
68

79
Note that if you want to use Crashlytics, make sure your `firebase.nativescript.json` file has `"crashlytics": true` and `"crash_reporting": false`,
810
then remove the `platforms` folder so these changes are picked up.
11+
12+
## Crashlytics API
13+
14+
### `sendCrashLog`
15+
Send a native iOS or Android exception to Crashlytics.
16+
17+
```typescript
18+
// either import like this:
19+
import { crashlytics } from "nativescript-plugin-firebase"; // and do: crashlytics.sendCrashLog
20+
// or this:
21+
import { crashlytics as firebaseCrashlytics } from "nativescript-plugin-firebase"; // and do: firebaseCrashlytics.sendCrashLog
22+
// or this:
23+
import * as firebase from "nativescript-plugin-firebase"; // and do: firebase.crashlytics.sendCrashLog
24+
25+
import { isAndroid, isIOS } from "tns-core-modules/platform";
26+
27+
if (isAndroid) {
28+
crashlytics.sendCrashLog(new java.lang.Exception("test Exception"));
29+
} else if (isIOS) {
30+
crashlytics.sendCrashLog(new NSError({
31+
domain: 'ShiploopHttpResponseErrorDomain',
32+
code: 42,
33+
userInfo: null
34+
}));
35+
}
36+
```
37+
38+
### `set*`
39+
Set a value that will be logged with an error and showing in the Firebase console on the 'Keys' tab of the error details.
40+
41+
```typescript
42+
import { crashlytics } from "nativescript-plugin-firebase"; // and do: crashlytics.sendCrashLog
43+
44+
crashlytics.setString("test_key", "test_value");
45+
crashlytics.setBool("test_key_bool", true);
46+
crashlytics.setInt("test_key_int", 2);
47+
crashlytics.setDouble("test_key_double", 56615.55548465);
48+
crashlytics.setFloat("test_key", 54646.45);
49+
50+
crashlytics.setUserId("user#42");
51+
```
49.5 KB
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
declare const com: any;
2+
3+
export function sendCrashLog(exception: any /* java.lang.Exception */): void {
4+
if (isCrashlyticsAvailable()) {
5+
com.crashlytics.android.Crashlytics.logException(exception);
6+
}
7+
}
8+
9+
export function setString(key: string, value: string): void {
10+
if (isCrashlyticsAvailable()) {
11+
com.crashlytics.android.Crashlytics.setString(key, value);
12+
}
13+
}
14+
15+
export function setBool(key: string, value: boolean): void {
16+
if (isCrashlyticsAvailable()) {
17+
com.crashlytics.android.Crashlytics.setBool(key, value);
18+
}
19+
}
20+
21+
export function setFloat(key: string, value: number): void {
22+
if (isCrashlyticsAvailable()) {
23+
com.crashlytics.android.Crashlytics.setFloat(key, value);
24+
}
25+
}
26+
27+
export function setInt(key: string, value: number): void {
28+
if (isCrashlyticsAvailable()) {
29+
com.crashlytics.android.Crashlytics.setInt(key, value);
30+
}
31+
}
32+
33+
export function setDouble(key: string, value: number): void {
34+
if (isCrashlyticsAvailable()) {
35+
com.crashlytics.android.Crashlytics.setDouble(key, value);
36+
}
37+
}
38+
39+
export function setUserId(id: string): void {
40+
if (isCrashlyticsAvailable()) {
41+
com.crashlytics.android.Crashlytics.setUserIdentifier(id);
42+
}
43+
}
44+
45+
function isCrashlyticsAvailable(): boolean {
46+
if (typeof (com.crashlytics) === "undefined" || typeof (com.crashlytics.android.Crashlytics) === "undefined") {
47+
console.log("Add 'crashlytics: true' to firebase.nativescript.json and remove the platforms folder");
48+
return false;
49+
}
50+
return true;
51+
}

src/crashlytics/crashlytics.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* send Crashlytics Log -> android Exception or iOS Exception
3+
* @param exception
4+
*/
5+
export declare function sendCrashLog(exception: any): void;
6+
7+
export declare function setString(key: string, value: string): void;
8+
9+
export declare function setBool(key: string, value: boolean): void;
10+
11+
export declare function setFloat(key: string, value: number): void;
12+
13+
export declare function setInt(key: string, value: number): void;
14+
15+
export declare function setDouble(key: string, value: number): void;
16+
17+
export declare function setUserId(userId: string): void;

src/crashlytics/crashlytics.ios.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export function sendCrashLog(exception: any /* NSError */): void {
2+
if (isCrashlyticsAvailable()) {
3+
Crashlytics.sharedInstance().recordError(exception);
4+
}
5+
}
6+
7+
export function setString(key: string, value: string): void {
8+
if (isCrashlyticsAvailable()) {
9+
Crashlytics.sharedInstance().setObjectValueForKey(value, key);
10+
}
11+
}
12+
13+
export function setBool(key: string, value: boolean): void {
14+
if (isCrashlyticsAvailable()) {
15+
Crashlytics.sharedInstance().setBoolValueForKey(value, key);
16+
}
17+
}
18+
19+
export function setFloat(key: string, value: number): void {
20+
if (isCrashlyticsAvailable()) {
21+
Crashlytics.sharedInstance().setFloatValueForKey(value, key);
22+
}
23+
}
24+
25+
export function setInt(key: string, value: number): void {
26+
if (isCrashlyticsAvailable()) {
27+
Crashlytics.sharedInstance().setIntValueForKey(value, key);
28+
}
29+
}
30+
31+
export function setDouble(key: string, value: number): void {
32+
if (isCrashlyticsAvailable()) {
33+
Crashlytics.sharedInstance().setFloatValueForKey(value, key);
34+
}
35+
}
36+
37+
export function setUserId(id: string): void {
38+
if (isCrashlyticsAvailable()) {
39+
Crashlytics.sharedInstance().setUserIdentifier(id);
40+
}
41+
}
42+
43+
function isCrashlyticsAvailable(): boolean {
44+
if (typeof (Crashlytics) === "undefined") {
45+
console.log("Add 'crashlytics: true' to firebase.nativescript.json and remove the platforms folder");
46+
return false;
47+
}
48+
return true;
49+
}

src/crashlytics/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as crashlytz from "./crashlytics";
2+
export const crashlytics = crashlytz;

src/firebase-common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getString, setString } from "tns-core-modules/application-settings";
33
import { firestore } from "./firebase";
44
import * as admob from "./admob/admob";
55
import * as analytics from "./analytics/analytics";
6+
import * as crashlytics from "./crashlytics/crashlytics";
67
import * as storage from "./storage/storage";
78
import * as mlkit from "./mlkit";
89

@@ -33,6 +34,7 @@ export const firebase: any = {
3334
_dynamicLinkCallback: null,
3435
admob,
3536
analytics,
37+
crashlytics,
3638
storage,
3739
mlkit,
3840
firestore: {

0 commit comments

Comments
 (0)