Skip to content

Commit a74eb25

Browse files
committed
Resolved a Fatal Exception
1 parent ba5b950 commit a74eb25

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
plugins {
1919
id 'com.android.application'
20-
id 'com.google.gms.google-services'
2120
}
2221

2322
def getSwvProperties() {
@@ -98,3 +97,9 @@ dependencies {
9897
implementation 'com.journeyapps:zxing-android-embedded:4.3.0' // ZXing library for QR code scanning
9998
implementation 'androidx.biometric:biometric:1.1.0' // Biometric authentication library
10099
}
100+
101+
if (file('google-services.json').exists()) {
102+
apply plugin: 'com.google.gms.google-services'
103+
} else {
104+
println "swv-fcm-sync: google-services.json not found. Skipping Firebase Configuration."
105+
}

app/src/main/java/mgks/os/swv/Functions.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -417,28 +417,40 @@ public interface TokenCallback {
417417
}
418418

419419
// Get fresh firebase tokens
420-
public void fcm_token(final TokenCallback callback) {
421-
FirebaseMessaging.getInstance().getToken()
422-
.addOnSuccessListener(token -> {
423-
if (!SWVContext.ASWP_OFFLINE) {
424-
set_cookie("FCM_TOKEN=" + token);
425-
if (SWVContext.SWV_DEBUGMODE) {
426-
Log.d("SLOG_FCM_BAKED", "YES");
427-
Log.d("SLOG_COOKIES", get_cookies(SWVContext.ASWV_URL));
428-
}
429-
}
430-
SWVContext.fcm_token = token;
431-
if (SWVContext.SWV_DEBUGMODE) {
432-
Log.d("SLOG_REQ_FCM_TOKEN", token);
433-
}
434-
callback.onTokenReceived(token); // Pass token to callback
435-
})
436-
.addOnFailureListener(e -> {
437-
SWVContext.fcm_token = "";
438-
Log.e("SLOG_REQ_FCM_TOKEN", "FAILED", e);
439-
callback.onTokenFailed(e); // Pass exception to callback
440-
});
441-
}
420+
public void fcm_token(final TokenCallback callback) {
421+
try {
422+
// Check if Firebase is initialized before proceeding
423+
// This call throws IllegalStateException if google-services.json was missing
424+
FirebaseMessaging.getInstance().getToken()
425+
.addOnSuccessListener(token -> {
426+
if (!SWVContext.ASWP_OFFLINE) {
427+
set_cookie("FCM_TOKEN=" + token);
428+
if (SWVContext.SWV_DEBUGMODE) {
429+
Log.d("SLOG_FCM_BAKED", "YES");
430+
Log.d("SLOG_COOKIES", get_cookies(SWVContext.ASWV_URL));
431+
}
432+
}
433+
SWVContext.fcm_token = token;
434+
if (SWVContext.SWV_DEBUGMODE) {
435+
Log.d("SLOG_REQ_FCM_TOKEN", token);
436+
}
437+
if (callback != null) callback.onTokenReceived(token);
438+
})
439+
.addOnFailureListener(e -> {
440+
SWVContext.fcm_token = "";
441+
Log.e("SLOG_REQ_FCM_TOKEN", "FAILED", e);
442+
if (callback != null) callback.onTokenFailed(e);
443+
});
444+
} catch (IllegalStateException e) {
445+
// This catches the crash when google-services.json is missing
446+
Log.w("SWV_FCM", "Firebase not initialized. Setup skipped (google-services.json missing).");
447+
if (callback != null) callback.onTokenFailed(e);
448+
} catch (Exception e) {
449+
// Catch generic errors
450+
Log.e("SWV_FCM", "Error initializing FCM", e);
451+
if (callback != null) callback.onTokenFailed(e);
452+
}
453+
}
442454

443455
// Injecting Google Analytics (gtag.js)
444456
public void inject_gtag(WebView webView, String gaId) {

0 commit comments

Comments
 (0)