diff --git a/lib/features/push_notification/presentation/config/fcm_configuration.dart b/lib/features/push_notification/presentation/config/fcm_configuration.dart index 33bf74f67..27321cd2a 100644 --- a/lib/features/push_notification/presentation/config/fcm_configuration.dart +++ b/lib/features/push_notification/presentation/config/fcm_configuration.dart @@ -14,5 +14,6 @@ class FcmConfiguration { FcmReceiver.instance.onForegroundMessage(); FcmReceiver.instance.onBackgroundMessage(); FcmReceiver.instance.getFcmToken(); + FcmReceiver.instance.onRefreshFcmToken(); } } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index 6d2881805..60edb7b79 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -28,5 +28,12 @@ class FcmReceiver { void getFcmToken() async { final token = await FirebaseMessaging.instance.getToken(); log('FcmReceiver::onFcmToken():token: $token'); + if (token?.isNotEmpty == true) { + FcmService.instance.handleRefreshToken(token!); + } + } + + void onRefreshFcmToken() { + FirebaseMessaging.instance.onTokenRefresh.listen(FcmService.instance.handleRefreshToken); } } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/services/fcm_service.dart b/lib/features/push_notification/presentation/services/fcm_service.dart index 62e1326b1..487b09927 100644 --- a/lib/features/push_notification/presentation/services/fcm_service.dart +++ b/lib/features/push_notification/presentation/services/fcm_service.dart @@ -16,6 +16,9 @@ class FcmService { final StreamController backgroundMessageStreamController = StreamController.broadcast(); Stream get backgroundMessageStream => backgroundMessageStreamController.stream; + final StreamController fcmTokenStreamController = StreamController.broadcast(); + Stream get fcmTokenStream => fcmTokenStreamController.stream; + FirebaseToken? currentToken; FcmService._internal(); @@ -35,6 +38,10 @@ class FcmService { backgroundMessageStreamController.add(newRemoteMessage); } + void handleRefreshToken(String newToken) { + fcmTokenStreamController.add(newToken); + } + void _closeStream() { foregroundMessageStreamController.close(); backgroundMessageStreamController.close();