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

Commit 7418905

Browse files
Opt-Out / Opt-In for Crashlytics - GDPR #1249
1 parent ea33ca1 commit 7418905

File tree

16 files changed

+1380
-8
lines changed

16 files changed

+1380
-8
lines changed

demo/app/main-view-model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ export class HelloWorldModel extends Observable {
421421

422422
public doInit(): void {
423423
firebase.init({
424+
crashlyticsCollectionEnabled: true,
424425
// storageBucket: 'gs://n-plugin-test.appspot.com',
425426
persist: true, // optional, default false
426427
// analyticsCollectionEnabled: false, // default true

demo/app_resources/Android/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
android:name="com.google.android.gms.ads.APPLICATION_ID"
3030
android:value="ca-app-pub-9517346003011652~4795634921"/>
3131

32+
<meta-data
33+
android:name="firebase_crashlytics_collection_enabled"
34+
android:value="false" />
35+
3236
<meta-data
3337
android:name="com.facebook.sdk.ApplicationId"
3438
android:value="@string/facebook_app_id"/>

demo/app_resources/iOS/Info.plist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,9 @@
104104
<string>Required for Firebase Invites</string>
105105
<key>GADApplicationIdentifier</key>
106106
<string>ca-app-pub-9517346003011652~2508636525</string>
107+
108+
<!-- we enable this during init, but can also do it through crashlytics.setCrashlyticsCollectionEnabled(true) after requesting the user to opt-in -->
109+
<key>firebase_crashlytics_collection_enabled</key>
110+
<string>false</string>
107111
</dict>
108112
</plist>

publish/scripts/installer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ dependencies {
768768
769769
// Cloud Messaging (FCM)
770770
` + (isSelected(result.messaging) || externalPushClientOnly ? `` : `//`) + ` implementation "com.google.firebase:firebase-messaging:17.6.0"
771+
// ` + (isSelected(result.messaging) || externalPushClientOnly ? `` : `//`) + ` implementation "me.leolin:ShortcutBadger:1.1.22@aar"
771772
772773
// In-App Messaging
773774
` + (isSelected(result.in_app_messaging) ? `` : `//`) + ` implementation "com.google.firebase:firebase-inappmessaging-display:17.1.1"

src/analytics/analytics.android.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function logComplexEvent(options: LogComplexEventOptions): Promise<void>
9393
}
9494

9595
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
96-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
96+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
9797
).logEvent(options.key, bundle);
9898

9999
resolve();
@@ -112,7 +112,8 @@ export function setUserId(arg): Promise<void> {
112112
return;
113113
}
114114

115-
com.google.firebase.analytics.FirebaseAnalytics.getInstance(appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()).setUserId(arg.userId);
115+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
116+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()).setUserId(arg.userId);
116117

117118
resolve();
118119
} catch (ex) {
@@ -135,7 +136,7 @@ export function setUserProperty(options: SetUserPropertyOptions): Promise<void>
135136
}
136137

137138
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
138-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
139+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
139140
).setUserProperty(options.key, options.value);
140141

141142
resolve();
@@ -155,7 +156,7 @@ export function setScreenName(options: SetScreenNameOptions): Promise<void> {
155156
}
156157

157158
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
158-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
159+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
159160
).setCurrentScreen(appModule.android.foregroundActivity, options.screenName, null);
160161

161162
resolve();
@@ -168,12 +169,12 @@ export function setScreenName(options: SetScreenNameOptions): Promise<void> {
168169

169170
export function setAnalyticsCollectionEnabled(enabled: boolean): void {
170171
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
171-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
172+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
172173
).setAnalyticsCollectionEnabled(enabled);
173174
}
174175

175176
export function setSessionTimeoutDuration(seconds: number): void {
176177
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
177-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
178+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
178179
).setSessionTimeoutDuration(seconds * 1000); // Android expects ms
179180
}

src/crashlytics/crashlytics.android.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as appModule from "tns-core-modules/application";
2+
13
declare const com: any;
24

35
export function sendCrashLog(exception: any /* java.lang.Exception */): void {
@@ -58,6 +60,14 @@ export function crash(): void {
5860
}
5961
}
6062

63+
export function setCrashlyticsCollectionEnabled(enabled: boolean): void {
64+
if (isCrashlyticsAvailable()) {
65+
io.fabric.sdk.android.Fabric.with(
66+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance(),
67+
[new com.crashlytics.android.Crashlytics()]);
68+
}
69+
}
70+
6171
function isCrashlyticsAvailable(): boolean {
6272
if (typeof (com.crashlytics) === "undefined" || typeof (com.crashlytics.android.Crashlytics) === "undefined") {
6373
console.log("Add 'crashlytics: true' to firebase.nativescript.json and remove the platforms folder");

src/crashlytics/crashlytics.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ export declare function setDouble(key: string, value: number): void;
1818

1919
export declare function setUserId(userId: string): void;
2020

21-
export declare function crash(): void;
21+
export declare function crash(): void;
22+
23+
export declare function setCrashlyticsCollectionEnabled(enabled: boolean): void;

src/crashlytics/crashlytics.ios.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export function crash(): void {
5454
if (isCrashlyticsAvailable()) {
5555
Crashlytics.sharedInstance().crash();
5656
}
57+
Fabric.with(NSArray.arrayWithObject(Crashlytics.class()));
58+
}
59+
60+
export function setCrashlyticsCollectionEnabled(enabled: boolean): void {
61+
if (isCrashlyticsAvailable()) {
62+
Fabric.with(NSArray.arrayWithObject(Crashlytics.class()));
63+
}
5764
}
5865

5966
function isCrashlyticsAvailable(): boolean {

src/firebase.android.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ firebase.init = arg => {
319319
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
320320
).setAnalyticsCollectionEnabled(arg.analyticsCollectionEnabled !== false);
321321

322+
// note that this only makes sense if crash reporting was disabled in AndroidManifest.xml
323+
if (arg.crashlyticsCollectionEnabled && typeof (com.crashlytics) !== "undefined" && typeof (com.crashlytics.android.Crashlytics) !== "undefined") {
324+
io.fabric.sdk.android.Fabric.with(
325+
appModule.android.context || com.tns.NativeScriptApplication.getInstance(),
326+
[new com.crashlytics.android.Crashlytics()]);
327+
}
328+
329+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
330+
appModule.android.context || com.tns.NativeScriptApplication.getInstance()
331+
).setAnalyticsCollectionEnabled(arg.analyticsCollectionEnabled !== false);
332+
322333
if (typeof (com.google.firebase.database) !== "undefined" && typeof (com.google.firebase.database.ServerValue) !== "undefined") {
323334
firebase.ServerValue = {
324335
TIMESTAMP: firebase.toJsObject(com.google.firebase.database.ServerValue.TIMESTAMP)

src/firebase.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,33 @@ export interface InitOptions extends MessagingOptions {
125125
* Default true.
126126
*/
127127
analyticsCollectionEnabled?: boolean;
128+
129+
/**
130+
* Allow the app to collect Crashlytics data and send it to Firebase.
131+
* Can also be set later with crashlytics.setCrashReportingEnabled.
132+
* Only useful in case it was disabled in AndroidManfifest.xml and/or Info.plist,
133+
* see https://firebase.google.com/docs/crashlytics/customize-crash-reports
134+
*/
135+
crashlyticsCollectionEnabled?: boolean;
136+
128137
/**
129138
* Allow disk persistence. Default true for Firestore, false for regular Firebase DB.
130139
*/
131140
persist?: boolean;
141+
132142
/**
133143
* Get notified when the user is logged in.
134144
*/
135145
onAuthStateChanged?: (data: AuthStateData) => void;
146+
136147
/**
137148
* Attempt to sign out before initializing, useful in case previous
138149
* project token is cached which leads to following type of error:
139150
* "[FirebaseDatabase] Authentication failed: invalid_token ..."
140151
* Default false.
141152
*/
142153
iOSEmulatorFlush?: boolean;
154+
143155
/**
144156
* For Firebase Storage you can pass in something like 'gs://n-plugin-test.appspot.com'
145157
* here so we can cache it. Otherwise pass in the 'bucket' param when using Storage features.

0 commit comments

Comments
 (0)