TF-1217 Handle fcm message on foreground/background/terminate

This commit is contained in:
dab246
2022-11-23 15:14:37 +07:00
committed by Dat H. Pham
parent d73b2bc1be
commit 68fb9de326
13 changed files with 528 additions and 19 deletions
@@ -0,0 +1,43 @@
import 'package:core/utils/app_logger.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';
int semaphoreBackground = 0;
@pragma('vm:entry-point')
Future<void> handleFirebaseBackgroundMessage(RemoteMessage message) async {
if (semaphoreBackground != 0) {
return;
}
semaphoreBackground = 1;
Future.delayed(const Duration(milliseconds: FcmService.durationMessageComing)).then((_) => semaphoreBackground = 0);
log('FcmReceiver::handleFirebaseBackgroundMessage(): ${message.data}');
FcmService.instance.handleFirebaseBackgroundMessage(message);
}
class FcmReceiver {
FcmReceiver._internal();
static final FcmReceiver _instance = FcmReceiver._internal();
static FcmReceiver get instance => _instance;
void onForegroundMessage() {
FirebaseMessaging.onMessage.listen(FcmService.instance.handleFirebaseForegroundMessage);
}
void onMessageOpenedApp() {
FirebaseMessaging.onMessageOpenedApp.listen(FcmService.instance.handleFirebaseMessageOpenedApp);
}
void onBackgroundMessage() {
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
}
void getFcmToken() async {
final token = await FirebaseMessaging.instance.getToken();
log('FcmReceiver::onFcmToken():token: $token');
}
}