Skip to content

Commit

Permalink
save fcm token to user_registered collection firestore, fix user regi…
Browse files Browse the repository at this point in the history
…st fields
  • Loading branch information
12henbx committed Nov 19, 2023
1 parent 46f3d94 commit a135b15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
29 changes: 23 additions & 6 deletions app/lib/api/firebase_api.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,13 +25,29 @@ class FirebaseApi {
await FirebaseMessaging.instance.getInitialMessage().then(handleMessage);
FirebaseMessaging.onMessageOpenedApp.listen(handleMessage);
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);

}

Future<void> initNotifications() async {
await _firebaseMessaging.requestPermission();
final fCMToken = await _firebaseMessaging.getToken();
print('Token: $fCMToken');
await initPushNotfications();
await _firebaseMessaging.requestPermission();

FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
Map<String, dynamic> deviceToken = {
'fcm_token': fcmToken,
'fcm_timestamp': FieldValue.serverTimestamp(),
};

// Reference to the Firestore collection
CollectionReference<Map<String, dynamic>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ class _RegisterPageState extends State<RegisterPage> {
(val) {
return state.province.error;
},
state.province.value,
state.province.value.isNotEmpty
? state.province.value
: 'Provinsi',
),
BiroBebrasDropdown(
'Bebras Biro',
Expand All @@ -176,7 +178,9 @@ class _RegisterPageState extends State<RegisterPage> {
(val) {
return state.bebrasBiro.error;
},
state.bebrasBiro.value,
state.bebrasBiro.value.isNotEmpty
? state.bebrasBiro.value
: 'Bebras Biro',
),
const SizedBox(height: 20),
BlocConsumer<UserRegisterBloc, RegisterFormState>(
Expand Down
8 changes: 0 additions & 8 deletions app/lib/services/firebase_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Future<void> handleBackgroundMessage(RemoteMessage message) async {

class FirebaseService {
static late FirebaseApp app;
final _firebaseMessaging = FirebaseMessaging.instance;

static Future<void> initialize() async {
app = await Firebase.initializeApp(
Expand All @@ -27,11 +26,4 @@ class FirebaseService {
static FirebaseAuth auth() {
return FirebaseAuth.instanceFor(app: app);
}

Future<void> initNotifications() async {
await _firebaseMessaging.requestPermission();
final fCMToken = await _firebaseMessaging.getToken();
print('Token: $fCMToken');
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
}
}

0 comments on commit a135b15

Please sign in to comment.