Initialize Firebase at startup so iOS APNs token is captured

Firebase was only initialized lazily when the user toggled push on. On iOS the
firebase_messaging plugin installs its APNs swizzling at app launch, but the
delivered device token can only be forwarded to FCM if a FirebaseApp is already
configured at that point. With lazy init there was none, so getAPNSToken() never
resolved and enabling push failed with "Could not enable push".

Add initFirebaseForPush() (mobile-only, idempotent) and call it from main() at
launch. Extract a shared pushSupportedOnThisPlatform getter so isSupported and
the initializer stay in sync; _ensureFirebase() now delegates to it as a safety
net.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-09 11:51:28 -04:00
co-authored by Claude Opus 4.8
parent 7d7f0197cd
commit 3d190e847e
2 changed files with 35 additions and 14 deletions
+10
View File
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:frontend/theme/catppuccin_mocha_theme.dart';
import 'package:frontend/utils/local_network_permission.dart';
import 'package:frontend/utils/pref_utils.dart';
import 'package:frontend/utils/push_service.dart';
import 'package:provider/provider.dart';
import 'navigation.dart';
import 'theme/gruvbox_theme.dart';
@@ -31,6 +32,15 @@ void main() {
);
}
// Initialize Firebase at launch so the firebase_messaging plugin's iOS
// APNs swizzling has a configured app to forward the device token to;
// without this getAPNSToken() never resolves and enabling push fails.
try {
await initFirebaseForPush();
} catch (e, stack) {
debugPrint('Firebase init for push failed, continuing: $e\n$stack');
}
// Settle iOS's local-network permission now, at launch, so it isn't
// still being decided when the user first taps "Test" in settings.
unawaited(requestLocalNetworkPermission());