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

Commit 8ce8bd4

Browse files
Merge pull request #967 from pboulch/master
Added Log with Crashlytics
2 parents 4a04c34 + 75eb186 commit 8ce8bd4

File tree

5 files changed

+259
-7
lines changed

5 files changed

+259
-7
lines changed

demo/app/main-page.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<TabViewItem.view>
8686
<ScrollView>
8787
<GridLayout columns="*, *"
88-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
88+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
8989
horizontalAlignment="stretch"
9090
class="tab-content">
9191

@@ -214,10 +214,17 @@
214214
<Android>
215215
<Button row="44" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
216216
</Android>
217-
218-
<Label row="45" colSpan="2" text="Firebase Invites" class="subtitle"/>
219-
<Button row="46" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
220-
<Button row="46" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
217+
<Button row="45" col="0" text="log message crashlytics" tap="{{ doLogMessageCrashlytics }}" class="button button-crash"/>
218+
<Button row="45" col="1" text="set string crashlytics" tap="{{ doSetCrashlyticString }}" class="button button-crash"/>
219+
<Button row="46" col="0" text="set bool crashlytics" tap="{{ doSetCrashlyticBool }}" class="button button-crash"/>
220+
<Button row="46" col="1" text="set float crashlytics" tap="{{ doSetCrashlyticFloat }}" class="button button-crash"/>
221+
<Button row="47" col="0" text="set double crashlytics" tap="{{ doSetCrashlyticDouble }}" class="button button-crash"/>
222+
<Button row="47" col="1" text="set int crashlytics" tap="{{ doSetCrashlyticInt }}" class="button button-crash"/>
223+
<Button row="48" colSpan="2" text="SetUserId" tap="{{ doSetUserId }}" class="button button-messaging"/>
224+
225+
<Label row="49" colSpan="2" text="Firebase Invites" class="subtitle"/>
226+
<Button row="50" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
227+
<Button row="50" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
221228
</GridLayout>
222229

223230
</ScrollView>

demo/app/main-view-model.ts

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Observable } from "tns-core-modules/data/observable";
22
import { alert, prompt } from "tns-core-modules/ui/dialogs";
3-
import { isIOS } from "tns-core-modules/platform";
3+
import { isIOS, isAndroid } from "tns-core-modules/platform";
44
import * as firebase from "nativescript-plugin-firebase";
55
import { AddEventListenerResult, storage as firebaseStorage, admob as firebaseAdMob, User } from "nativescript-plugin-firebase";
66
import * as fs from "tns-core-modules/file-system";
@@ -1622,4 +1622,100 @@ export class HelloWorldModel extends Observable {
16221622
public doForceCrashAndroid(): void {
16231623
throw new java.lang.Exception("Forced an exception.");
16241624
}
1625+
1626+
public doLogMessageCrashlytics(): void {
1627+
if (isAndroid) {
1628+
// 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+
);
1645+
} else if (isIOS) {
1646+
// 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+
);
1663+
}
1664+
1665+
}
1666+
1667+
1668+
public doSetCrashlyticString(): void {
1669+
firebase.setCrashlyticsString("test_key", "test_value");
1670+
alert({
1671+
title: "String created",
1672+
message: "New string key created, log a new message and check firebase console",
1673+
okButtonText: "Okay"
1674+
});
1675+
}
1676+
1677+
public doSetCrashlyticBool(): void {
1678+
firebase.setCrashlyticsBool("test_key_bool", true);
1679+
alert({
1680+
title: "Bool created",
1681+
message: "New string key created, log a new message and check firebase console",
1682+
okButtonText: "Okay"
1683+
});
1684+
}
1685+
1686+
public doSetCrashlyticInt(): void {
1687+
firebase.setCrashlyticsInt("test_key_int", 2);
1688+
alert({
1689+
title: "Int created",
1690+
message: "New string key created, log a new message and check firebase console",
1691+
okButtonText: "Okay"
1692+
});
1693+
}
1694+
1695+
public doSetCrashlyticDouble(): void {
1696+
firebase.setCrashlyticsDouble("test_key_double", 56615.55548465);
1697+
alert({
1698+
title: "Double created",
1699+
message: "New string key created, log a new message and check firebase console",
1700+
okButtonText: "Okay"
1701+
});
1702+
}
1703+
1704+
public doSetCrashlyticFloat(): void {
1705+
firebase.setCrashlyticsFloat("test_key", 54646.45);
1706+
alert({
1707+
title: "Float created",
1708+
message: "New string key created, log a new message and check firebase console",
1709+
okButtonText: "Okay"
1710+
});
1711+
}
1712+
1713+
public doSetUserId(): void {
1714+
firebase.setUserIdCrashlytics("user#42");
1715+
alert({
1716+
title: "User id changed",
1717+
message: "Log a new message and check firebase console",
1718+
okButtonText: "Okay"
1719+
});
1720+
}
16251721
}

src/firebase.android.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,74 @@ firebase.sendCrashLog = arg => {
16851685
});
16861686
};
16871687

1688+
firebase.sendCrashlyticsLog = arg => {
1689+
return new Promise((resolve, reject) => {
1690+
try {
1691+
1692+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1693+
reject("Make sure Crashlytics is in the plugin's include.gradle");
1694+
return;
1695+
}
1696+
1697+
com.crashlytics.android.Crashlytics.logException(arg);
1698+
resolve();
1699+
} catch (ex) {
1700+
console.log("Error in firebase.sendCrashlyticsLog: " + ex);
1701+
reject(ex);
1702+
}
1703+
});
1704+
};
1705+
1706+
firebase.setCrashlyticsString = (key, value) => {
1707+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1708+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1709+
return;
1710+
}
1711+
com.crashlytics.android.Crashlytics.setString(key , value);
1712+
};
1713+
1714+
firebase.setCrashlyticsBool = (key, value) => {
1715+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1716+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1717+
return;
1718+
}
1719+
com.crashlytics.android.Crashlytics.setBool(key , value);
1720+
};
1721+
1722+
firebase.setCrashlyticsFloat = (key, value) => {
1723+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1724+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1725+
return;
1726+
}
1727+
com.crashlytics.android.Crashlytics.setFloat(key , value);
1728+
};
1729+
1730+
firebase.setCrashlyticsInt = (key, value) => {
1731+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1732+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1733+
return;
1734+
}
1735+
com.crashlytics.android.Crashlytics.setInt(key , value);
1736+
};
1737+
1738+
firebase.setCrashlyticsDouble = (key, value) => {
1739+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1740+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1741+
return;
1742+
}
1743+
com.crashlytics.android.Crashlytics.setDouble(key , value);
1744+
};
1745+
1746+
1747+
firebase.setUserIdCrashlytics = arg => {
1748+
if (typeof (com.crashlytics.android.Crashlytics) === "undefined") {
1749+
console.log("Error - Make sure Crashlytics is in the plugin's include.gradle");
1750+
return;
1751+
}
1752+
com.crashlytics.android.Crashlytics.setUserIdentifier(arg);
1753+
};
1754+
1755+
16881756
firebase.invites.sendInvitation = arg => {
16891757
return new Promise((resolve, reject) => {
16901758
try {

src/firebase.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,18 @@ export function getRemoteConfig(options: GetRemoteConfigOptions): Promise<GetRem
890890

891891
// crash logging
892892
export function sendCrashLog(options: SendCrashLogOptions): Promise<any>;
893+
894+
// send Crashlytics Log -> android Exception or iOS Exception
895+
export function sendCrashlyticsLog(exception: any) : Promise<any>;
896+
897+
export function setCrashlyticsString(key: string, value : string) : void;
898+
899+
export function setCrashlyticsBool(key : string, value : boolean) : void;
900+
901+
export function setCrashlyticsFloat(key : string, value : number) : void;
902+
903+
export function setCrashlyticsInt(key : string, value : number) : void;
904+
905+
export function setCrashlyticsDouble(key : string, value : number) : void;
906+
907+
export function setUserIdCrashlytics(userId : string) : void;

src/firebase.ios.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,6 @@ firebase.sendCrashLog = arg => {
14161416
return new Promise((resolve, reject) => {
14171417
try {
14181418
// TODO generate typings again and see if 'FIRCrashLog' is available
1419-
14201419
/*
14211420
if (typeof(FIRCrashLog) === "undefined") {
14221421
reject("Make sure 'Firebase/Crash' is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
@@ -1443,6 +1442,73 @@ firebase.sendCrashLog = arg => {
14431442
});
14441443
};
14451444

1445+
firebase.sendCrashlyticsLog = arg => {
1446+
return new Promise((resolve, reject) => {
1447+
try {
1448+
1449+
if (typeof (Crashlytics) === "undefined") {
1450+
reject("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1451+
return;
1452+
}
1453+
1454+
Crashlytics.sharedInstance().recordError(arg);
1455+
resolve();
1456+
} catch (ex) {
1457+
console.log("Error in firebase.sendCrashlyticsLog: " + ex);
1458+
reject(ex);
1459+
}
1460+
});
1461+
};
1462+
1463+
firebase.setCrashlyticsString = (key, value) => {
1464+
if (typeof (Crashlytics) === "undefined") {
1465+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1466+
return;
1467+
}
1468+
Crashlytics.sharedInstance().setObjectValueForKey(value, key);
1469+
};
1470+
1471+
firebase.setCrashlyticsBool = (key, value) => {
1472+
if (typeof (Crashlytics) === "undefined") {
1473+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1474+
return;
1475+
}
1476+
Crashlytics.sharedInstance().setBoolValueForKey(value, key);
1477+
};
1478+
1479+
firebase.setCrashlyticsFloat = (key, value) => {
1480+
if (typeof (Crashlytics) === "undefined") {
1481+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1482+
return;
1483+
}
1484+
Crashlytics.sharedInstance().setFloatValueForKey(value , key);
1485+
};
1486+
1487+
firebase.setCrashlyticsInt = (key, value) => {
1488+
if (typeof (Crashlytics) === "undefined") {
1489+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1490+
return;
1491+
}
1492+
Crashlytics.sharedInstance().setIntValueForKey(value, key);
1493+
};
1494+
1495+
firebase.setCrashlyticsDouble = (key, value) => {
1496+
if (typeof (Crashlytics) === "undefined") {
1497+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1498+
return;
1499+
}
1500+
Crashlytics.sharedInstance().setFloatValueForKey(key , value);
1501+
};
1502+
1503+
1504+
firebase.setUserIdCrashlytics = arg => {
1505+
if (typeof (Crashlytics) === "undefined") {
1506+
console.log("Make sure Crashlytics is in the plugin's Podfile - and if it is there's currently a problem with this Pod which is outside out span of control :(");
1507+
return;
1508+
}
1509+
Crashlytics.sharedInstance().setUserIdentifier(arg);
1510+
};
1511+
14461512
firebase.invites.sendInvitation = arg => {
14471513
return new Promise((resolve, reject) => {
14481514
try {

0 commit comments

Comments
 (0)