Fix Android crash: don't register push without Firebase

On Android, PushNotifications.register() calls FirebaseMessaging.getInstance(),
which throws "Default FirebaseApp is not initialized" when the build has no
google-services.json. Capacitor runs the plugin method on its own thread, so the
exception is an UNCAUGHT NATIVE crash a JS try/catch can't stop — the app died
right after sign-in and on every relaunch.

Gate Android push registration on a new server flag: /api/meetings/config now
returns fcm (push.fcmReady() = FCM_SERVICE_ACCOUNT present). setupNativePush skips
register() on Android unless fcm is true. Web-side fix — deploys without a rebuild;
push auto-enables once Firebase (client google-services.json + server FCM) is set up.
iOS/APNs unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 21:55:09 +05:30
parent b03a32b10d
commit a4726dd10c
3 changed files with 15 additions and 2 deletions
+9
View File
@@ -4234,6 +4234,15 @@ let _nativeTok=null;
async function setupNativePush(){
const PN=capPlugin('PushNotifications'); const plat=nativePlatform();
if(!PN || (plat!=='ios' && plat!=='android')) return false; // not a mobile native app
// Android push needs Firebase: google-services.json in the build AND FCM on the server. Without it,
// PushNotifications.register() throws "Default FirebaseApp is not initialized" — an UNCAUGHT NATIVE crash
// (thrown on Capacitor's plugin thread; a JS try/catch can't stop it, it kills the app on every launch).
// So on Android, only register when the server reports FCM is configured (we add google-services.json to the
// build + FCM_SERVICE_ACCOUNT to the server together). iOS (APNs) is unaffected and registers as before.
if(plat==='android'){
let cfg={}; try{ cfg=await fetch('/api/meetings/config').then(r=>r.json()); }catch(_){}
if(!cfg || !cfg.fcm){ console.log('[push] Android FCM not configured on the server — skipping native push registration (avoids the FirebaseApp-not-initialized crash)'); return true; }
}
try{
PN.addListener('registration', async (t)=>{ const token=t&&t.value; if(!token) return; _nativeTok=token;
try{ await postJSON('/api/v1/devices',{ platform:plat, token }); pushActive=true; console.log('[push] native device registered'); }