FCM push Notifications
This commit is contained in:
+26
-26
@@ -22,17 +22,12 @@ import 'services/notification_bridge.dart';
|
||||
|
||||
/// Handle messages received while the app is terminated or in background.
|
||||
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
// initialize plugin in background isolate
|
||||
await NotificationService.initialize();
|
||||
final notification = message.notification;
|
||||
if (notification != null) {
|
||||
NotificationService.show(
|
||||
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
title: notification.title ?? 'Notification',
|
||||
body: notification.body ?? '',
|
||||
payload: message.data['payload'],
|
||||
);
|
||||
}
|
||||
// Do minimal work in background isolate. Avoid initializing Flutter
|
||||
// plugins here (e.g. flutter_local_notifications) as that can hang
|
||||
// the background isolate and interfere with subsequent launches.
|
||||
// If you need to persist data from a background message, perform a
|
||||
// lightweight HTTP call or write to a background-capable DB.
|
||||
return;
|
||||
}
|
||||
|
||||
Future<void> main() async {
|
||||
@@ -58,22 +53,9 @@ Future<void> main() async {
|
||||
|
||||
await Supabase.initialize(url: supabaseUrl, anonKey: supabaseAnonKey);
|
||||
|
||||
// ensure token saved right away if already signed in
|
||||
// ensure token saved shortly after startup if already signed in.
|
||||
// Run this after runApp so startup is not blocked by network/token ops.
|
||||
final supaClient = Supabase.instance.client;
|
||||
String? initialToken;
|
||||
if (!kIsWeb) {
|
||||
try {
|
||||
initialToken = await FirebaseMessaging.instance.getToken();
|
||||
} catch (e) {
|
||||
debugPrint('FCM getToken failed: $e');
|
||||
initialToken = null;
|
||||
}
|
||||
if (initialToken != null && supaClient.auth.currentUser != null) {
|
||||
debugPrint('initial FCM token for signed-in user: $initialToken');
|
||||
final ctrl = NotificationsController(supaClient);
|
||||
await ctrl.registerFcmToken(initialToken);
|
||||
}
|
||||
}
|
||||
|
||||
// listen for auth changes to register/unregister token accordingly
|
||||
supaClient.auth.onAuthStateChange.listen((data) async {
|
||||
@@ -152,6 +134,24 @@ Future<void> main() async {
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Post-startup: register current FCM token without blocking UI.
|
||||
if (!kIsWeb) {
|
||||
Future.microtask(() async {
|
||||
try {
|
||||
final token = await FirebaseMessaging.instance.getToken().timeout(
|
||||
const Duration(seconds: 10),
|
||||
);
|
||||
if (token != null && supaClient.auth.currentUser != null) {
|
||||
debugPrint('post-startup registering FCM token: $token');
|
||||
final ctrl = NotificationsController(supaClient);
|
||||
await ctrl.registerFcmToken(token);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('post-startup FCM token registration failed: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationSoundObserver extends ProviderObserver {
|
||||
|
||||
Reference in New Issue
Block a user