TF-439 Implement get firebase subscription

This commit is contained in:
dab246
2022-11-29 15:34:06 +07:00
committed by Dat H. Pham
parent 38a6067319
commit 4de25a778c
13 changed files with 231 additions and 28 deletions
@@ -1,8 +1,11 @@
import 'package:core/data/model/source_type/data_source_type.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/fcm_datasource_impl.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/hive_fcm_datasource_impl.dart';
import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart';
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
import 'package:tmail_ui_user/features/push_notification/data/repository/fcm_repository_impl.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_email_state_to_refresh_interactor.dart';
@@ -10,6 +13,7 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_changes_to_push_notification_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_email_state_to_refresh_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_fcm_token_cache_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_firebase_subscription_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_stored_email_delivery_state_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/save_fcm_token_cache_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_device_id_interactor.dart';
@@ -26,7 +30,7 @@ class FcmInteractorBindings extends InteractorsBindings {
@override
void bindingsDataSource() {
Get.lazyPut<FCMDatasource>(() => Get.find<HiveFCMDatasourceImpl>());
Get.lazyPut<FCMDatasource>(() => Get.find<FcmDatasourceImpl>());
Get.lazyPut<ThreadDataSource>(() => Get.find<ThreadDataSourceImpl>());
}
@@ -36,6 +40,10 @@ class FcmInteractorBindings extends InteractorsBindings {
Get.find<FCMCacheManager>(),
Get.find<CacheExceptionThrower>(),
));
Get.lazyPut(() => FcmDatasourceImpl(
Get.find<FcmApi>(),
Get.find<RemoteExceptionThrower>(),
));
Get.lazyPut(() => ThreadDataSourceImpl(
Get.find<ThreadAPI>(),
Get.find<ThreadIsolateWorker>(),
@@ -55,6 +63,7 @@ class FcmInteractorBindings extends InteractorsBindings {
Get.lazyPut(() => GetEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => DeleteEmailStateToRefreshInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => StoreDeviceIdInteractor(Get.find<FCMRepositoryImpl>()));
Get.lazyPut(() => GetFirebaseSubscriptionInteractor(Get.find<FCMRepositoryImpl>()));
}
@override
@@ -65,7 +74,10 @@ class FcmInteractorBindings extends InteractorsBindings {
@override
void bindingsRepositoryImpl() {
Get.lazyPut(() => FCMRepositoryImpl(
Get.find<FCMDatasource>(),
{
DataSourceType.local: Get.find<HiveFCMDatasourceImpl>(),
DataSourceType.network: Get.find<FCMDatasource>(),
},
Get.find<ThreadDataSource>()
));
}
@@ -4,9 +4,11 @@ import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:fcm/model/firebase_token.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/get_firebase_subscription_state.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_firebase_subscription_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_device_id_interactor.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
class FcmTokenHandler {
@@ -17,22 +19,35 @@ class FcmTokenHandler {
static FcmTokenHandler get instance => _instance;
StoreDeviceIdInteractor? _storeDeviceIdInteractor;
GetFirebaseSubscriptionInteractor? _getFirebaseSubscriptionInteractor;
FirebaseToken? _fcmToken;
String? _deviceId;
void initialize() {
try {
_storeDeviceIdInteractor = Get.find<StoreDeviceIdInteractor>();
_storeDeviceIdInteractor = getBinding<StoreDeviceIdInteractor>();
_getFirebaseSubscriptionInteractor = getBinding<GetFirebaseSubscriptionInteractor>();
} catch (e) {
logError('FcmTokenHandler::initialize(): ${e.toString()}');
}
}
void handle(String token) {
void handle(String? token) {
log('FcmTokenHandler::handle():token: $token');
final fcmToken = FirebaseToken(token);
final deviceId = FcmUtils.instance.hashTokenToDeviceId(token);
log('FcmTokenHandler::handle(): fcmToken: $fcmToken');
log('FcmTokenHandler::handle(): deviceId: $deviceId');
_storeDeviceIdAction(deviceId);
if (token != null) {
_fcmToken = FirebaseToken(token);
_deviceId = FcmUtils.instance.hashTokenToDeviceId(token);
log('FcmTokenHandler::handle(): fcmToken: $_fcmToken');
log('FcmTokenHandler::handle(): deviceId: $_deviceId');
_getFcmTokenFromBackend(_deviceId!);
}
}
void _getFcmTokenFromBackend(String deviceId) {
if (_getFirebaseSubscriptionInteractor != null) {
_consumeState(_getFirebaseSubscriptionInteractor!.execute(deviceId));
}
}
void _storeDeviceIdAction(String deviceId) {
@@ -57,9 +72,16 @@ class FcmTokenHandler {
void _handleFailureViewState(Failure failure) {
log('FcmTokenHandler::_handleFailureViewState(): $failure');
if (failure is GetFirebaseSubscriptionFailure) {
// Register new token
}
}
void _handleSuccessViewState(Success success) {
log('FcmTokenHandler::_handleSuccessViewState(): $success');
if (success is GetFirebaseSubscriptionSuccess) {
final token = success.firebaseSubscription.token;
log('FcmTokenHandler::_handleSuccessViewState():token: $token');
}
}
}
@@ -28,9 +28,7 @@ class FcmReceiver {
void getFcmToken() async {
final token = await FirebaseMessaging.instance.getToken();
log('FcmReceiver::onFcmToken():token: $token');
if (token?.isNotEmpty == true) {
FcmService.instance.handleRefreshToken(token!);
}
FcmService.instance.handleRefreshToken(token);
}
void onRefreshFcmToken() {
@@ -15,8 +15,8 @@ class FcmService {
final StreamController<RemoteMessage> backgroundMessageStreamController = StreamController<RemoteMessage>.broadcast();
Stream<RemoteMessage> get backgroundMessageStream => backgroundMessageStreamController.stream;
final StreamController<String> fcmTokenStreamController = StreamController<String>.broadcast();
Stream<String> get fcmTokenStream => fcmTokenStreamController.stream;
final StreamController<String?> fcmTokenStreamController = StreamController<String?>.broadcast();
Stream<String?> get fcmTokenStream => fcmTokenStreamController.stream;
FcmService._internal();
@@ -35,13 +35,14 @@ class FcmService {
backgroundMessageStreamController.add(newRemoteMessage);
}
void handleRefreshToken(String newToken) {
void handleRefreshToken(String? newToken) {
fcmTokenStreamController.add(newToken);
}
void _closeStream() {
foregroundMessageStreamController.close();
backgroundMessageStreamController.close();
fcmTokenStreamController.close();
}
void dispose() {