diff --git a/app/lib/api/firebase_api.dart b/app/lib/api/firebase_api.dart index b660878..688d68a 100644 --- a/app/lib/api/firebase_api.dart +++ b/app/lib/api/firebase_api.dart @@ -1,11 +1,12 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import '../services/firebase_service.dart'; import '../services/router_service.dart'; - class FirebaseApi { final _firebaseMessaging = FirebaseMessaging.instance; + final currentUser = FirebaseService.auth().currentUser; void handleMessage(RemoteMessage? message) { if (message == null) return; @@ -24,13 +25,29 @@ class FirebaseApi { await FirebaseMessaging.instance.getInitialMessage().then(handleMessage); FirebaseMessaging.onMessageOpenedApp.listen(handleMessage); FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage); - } Future initNotifications() async { - await _firebaseMessaging.requestPermission(); - final fCMToken = await _firebaseMessaging.getToken(); - print('Token: $fCMToken'); - await initPushNotfications(); + await _firebaseMessaging.requestPermission(); + + FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) { + Map deviceToken = { + 'fcm_token': fcmToken, + 'fcm_timestamp': FieldValue.serverTimestamp(), + }; + + // Reference to the Firestore collection + CollectionReference> fcmTokensCollection = + FirebaseFirestore.instance.collection('registered_user'); + + // Set the device token in Firestore + fcmTokensCollection.doc(currentUser!.uid).update(deviceToken); + }).onError((err) { + // Error getting token. + }); + + final fCMToken = await _firebaseMessaging.getToken(); + print('Token: $fCMToken'); + await initPushNotfications(); } } diff --git a/app/lib/features/authentication/register/presentation/pages/register_page.dart b/app/lib/features/authentication/register/presentation/pages/register_page.dart index ee06603..8fbaa26 100644 --- a/app/lib/features/authentication/register/presentation/pages/register_page.dart +++ b/app/lib/features/authentication/register/presentation/pages/register_page.dart @@ -160,7 +160,9 @@ class _RegisterPageState extends State { (val) { return state.province.error; }, - state.province.value, + state.province.value.isNotEmpty + ? state.province.value + : 'Provinsi', ), BiroBebrasDropdown( 'Bebras Biro', @@ -176,7 +178,9 @@ class _RegisterPageState extends State { (val) { return state.bebrasBiro.error; }, - state.bebrasBiro.value, + state.bebrasBiro.value.isNotEmpty + ? state.bebrasBiro.value + : 'Bebras Biro', ), const SizedBox(height: 20), BlocConsumer( diff --git a/app/lib/services/firebase_service.dart b/app/lib/services/firebase_service.dart index 0d50fd2..236c8d1 100644 --- a/app/lib/services/firebase_service.dart +++ b/app/lib/services/firebase_service.dart @@ -12,7 +12,6 @@ Future handleBackgroundMessage(RemoteMessage message) async { class FirebaseService { static late FirebaseApp app; - final _firebaseMessaging = FirebaseMessaging.instance; static Future initialize() async { app = await Firebase.initializeApp( @@ -27,11 +26,4 @@ class FirebaseService { static FirebaseAuth auth() { return FirebaseAuth.instanceFor(app: app); } - - Future initNotifications() async { - await _firebaseMessaging.requestPermission(); - final fCMToken = await _firebaseMessaging.getToken(); - print('Token: $fCMToken'); - FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage); - } }