TF-3341 Enable web socket for mobile
This commit is contained in:
+4
-3
@@ -125,6 +125,7 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_
|
|||||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_mailbox_state_to_refresh_interactor.dart';
|
import 'package:tmail_ui_user/features/push_notification/domain/usecases/delete_mailbox_state_to_refresh_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_email_state_to_refresh_interactor.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_mailbox_state_to_refresh_interactor.dart';
|
import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_mailbox_state_to_refresh_interactor.dart';
|
||||||
|
import 'package:tmail_ui_user/features/push_notification/presentation/controller/web_socket_controller.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
|
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';
|
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
||||||
@@ -627,9 +628,8 @@ class MailboxDashBoardController extends ReloadableController with UserSettingPo
|
|||||||
injectAutoCompleteBindings(session, currentAccountId);
|
injectAutoCompleteBindings(session, currentAccountId);
|
||||||
injectRuleFilterBindings(session, currentAccountId);
|
injectRuleFilterBindings(session, currentAccountId);
|
||||||
injectVacationBindings(session, currentAccountId);
|
injectVacationBindings(session, currentAccountId);
|
||||||
if (PlatformInfo.isWeb) {
|
injectWebSocket(session, currentAccountId);
|
||||||
injectWebSocket(session, currentAccountId);
|
if (PlatformInfo.isMobile) {
|
||||||
} else {
|
|
||||||
injectFCMBindings(session, currentAccountId);
|
injectFCMBindings(session, currentAccountId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2948,6 +2948,7 @@ class MailboxDashBoardController extends ReloadableController with UserSettingPo
|
|||||||
sessionCurrent = null;
|
sessionCurrent = null;
|
||||||
mapMailboxById = {};
|
mapMailboxById = {};
|
||||||
mapDefaultMailboxIdByRole = {};
|
mapDefaultMailboxIdByRole = {};
|
||||||
|
WebSocketController.instance.onClose();
|
||||||
super.onClose();
|
super.onClose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,17 +59,9 @@ class FcmMessageController extends PushBaseController {
|
|||||||
super.initialize(accountId: accountId, session: session);
|
super.initialize(accountId: accountId, session: session);
|
||||||
|
|
||||||
_listenTokenStream();
|
_listenTokenStream();
|
||||||
_listenForegroundMessageStream();
|
|
||||||
_listenBackgroundMessageStream();
|
_listenBackgroundMessageStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _listenForegroundMessageStream() {
|
|
||||||
FcmService.instance.foregroundMessageStreamController
|
|
||||||
?.stream
|
|
||||||
.debounceTime(const Duration(milliseconds: FcmUtils.durationMessageComing))
|
|
||||||
.listen(_handleForegroundMessageAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _listenBackgroundMessageStream() {
|
void _listenBackgroundMessageStream() {
|
||||||
FcmService.instance.backgroundMessageStreamController
|
FcmService.instance.backgroundMessageStreamController
|
||||||
?.stream
|
?.stream
|
||||||
@@ -84,21 +76,6 @@ class FcmMessageController extends PushBaseController {
|
|||||||
.listen(FcmTokenController.instance.onFcmTokenChanged);
|
.listen(FcmTokenController.instance.onFcmTokenChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleForegroundMessageAction(Map<String, dynamic> payloadData) {
|
|
||||||
log('FcmMessageController::_handleForegroundMessageAction():payloadData: $payloadData | accountId: $accountId');
|
|
||||||
if (accountId != null && session?.username != null) {
|
|
||||||
final stateChange = FcmUtils.instance.convertFirebaseDataMessageToStateChange(payloadData);
|
|
||||||
final mapTypeState = stateChange.getMapTypeState(accountId!);
|
|
||||||
mappingTypeStateToAction(
|
|
||||||
mapTypeState,
|
|
||||||
accountId!,
|
|
||||||
emailChangeListener: EmailChangeListener.instance,
|
|
||||||
mailboxChangeListener: MailboxChangeListener.instance,
|
|
||||||
session!.username,
|
|
||||||
session: session);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _handleBackgroundMessageAction(Map<String, dynamic> payloadData) async {
|
void _handleBackgroundMessageAction(Map<String, dynamic> payloadData) async {
|
||||||
log('FcmMessageController::_handleBackgroundMessageAction():payloadData: $payloadData');
|
log('FcmMessageController::_handleBackgroundMessageAction():payloadData: $payloadData');
|
||||||
final stateChange = FcmUtils.instance.convertFirebaseDataMessageToStateChange(payloadData);
|
final stateChange = FcmUtils.instance.convertFirebaseDataMessageToStateChange(payloadData);
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ abstract class PushBaseController {
|
|||||||
this.session = session;
|
this.session = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onClose() {}
|
||||||
|
|
||||||
void mappingTypeStateToAction(
|
void mappingTypeStateToAction(
|
||||||
Map<String, dynamic> mapTypeState,
|
Map<String, dynamic> mapTypeState,
|
||||||
AccountId accountId,
|
AccountId accountId,
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import 'dart:convert';
|
|||||||
import 'package:core/presentation/state/failure.dart';
|
import 'package:core/presentation/state/failure.dart';
|
||||||
import 'package:core/presentation/state/success.dart';
|
import 'package:core/presentation/state/success.dart';
|
||||||
import 'package:core/utils/app_logger.dart';
|
import 'package:core/utils/app_logger.dart';
|
||||||
|
import 'package:core/utils/platform_info.dart';
|
||||||
import 'package:fcm/model/type_name.dart';
|
import 'package:fcm/model/type_name.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||||
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
||||||
@@ -33,13 +35,12 @@ class WebSocketController extends PushBaseController {
|
|||||||
WebSocketChannel? _webSocketChannel;
|
WebSocketChannel? _webSocketChannel;
|
||||||
Timer? _webSocketPingTimer;
|
Timer? _webSocketPingTimer;
|
||||||
StreamSubscription? _webSocketSubscription;
|
StreamSubscription? _webSocketSubscription;
|
||||||
|
AppLifecycleListener? _appLifecycleListener;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void handleFailureViewState(Failure failure) {
|
void handleFailureViewState(Failure failure) {
|
||||||
logError('WebSocketController::handleFailureViewState():Failure $failure');
|
logError('WebSocketController::handleFailureViewState():Failure $failure');
|
||||||
_webSocketSubscription?.cancel();
|
_cleanUpWebSocketResources();
|
||||||
_webSocketChannel = null;
|
|
||||||
_webSocketPingTimer?.cancel();
|
|
||||||
if (failure is WebSocketConnectionFailed) {
|
if (failure is WebSocketConnectionFailed) {
|
||||||
_handleWebSocketConnectionRetry();
|
_handleWebSocketConnectionRetry();
|
||||||
}
|
}
|
||||||
@@ -64,6 +65,31 @@ class WebSocketController extends PushBaseController {
|
|||||||
super.initialize(accountId: accountId, session: session);
|
super.initialize(accountId: accountId, session: session);
|
||||||
|
|
||||||
_connectWebSocket(accountId, session);
|
_connectWebSocket(accountId, session);
|
||||||
|
if (PlatformInfo.isMobile) {
|
||||||
|
_listenToAppLifeCycle(accountId, session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onClose() {
|
||||||
|
_cleanUpWebSocketResources();
|
||||||
|
_appLifecycleListener?.dispose();
|
||||||
|
_appLifecycleListener = null;
|
||||||
|
super.onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _listenToAppLifeCycle(AccountId? accountId, Session? session) {
|
||||||
|
_appLifecycleListener ??= AppLifecycleListener(
|
||||||
|
onStateChange: (appLifecycleState) {
|
||||||
|
switch (appLifecycleState) {
|
||||||
|
case AppLifecycleState.resumed:
|
||||||
|
_connectWebSocket(accountId, session);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_cleanUpWebSocketResources();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _connectWebSocket(AccountId? accountId, Session? session) {
|
void _connectWebSocket(AccountId? accountId, Session? session) {
|
||||||
@@ -75,8 +101,15 @@ class WebSocketController extends PushBaseController {
|
|||||||
consumeState(_connectWebSocketInteractor!.execute(session, accountId));
|
consumeState(_connectWebSocketInteractor!.execute(session, accountId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _cleanUpWebSocketResources() {
|
||||||
|
_webSocketSubscription?.cancel();
|
||||||
|
_webSocketChannel = null;
|
||||||
|
_webSocketPingTimer?.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
void _handleWebSocketConnectionSuccess(WebSocketConnectionSuccess success) {
|
void _handleWebSocketConnectionSuccess(WebSocketConnectionSuccess success) {
|
||||||
log('WebSocketController::_handleWebSocketConnectionSuccess(): $success');
|
log('WebSocketController::_handleWebSocketConnectionSuccess(): $success');
|
||||||
|
_cleanUpWebSocketResources();
|
||||||
_retryRemained = 3;
|
_retryRemained = 3;
|
||||||
_webSocketChannel = success.webSocketChannel;
|
_webSocketChannel = success.webSocketChannel;
|
||||||
_enableWebSocketPush();
|
_enableWebSocketPush();
|
||||||
@@ -87,9 +120,7 @@ class WebSocketController extends PushBaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _handleWebSocketConnectionRetry() {
|
void _handleWebSocketConnectionRetry() {
|
||||||
_webSocketSubscription?.cancel();
|
_cleanUpWebSocketResources();
|
||||||
_webSocketChannel = null;
|
|
||||||
_webSocketPingTimer?.cancel();
|
|
||||||
if (_retryRemained > 0) {
|
if (_retryRemained > 0) {
|
||||||
_retryRemained--;
|
_retryRemained--;
|
||||||
_connectWebSocket(accountId, session);
|
_connectWebSocket(accountId, session);
|
||||||
|
|||||||
@@ -20,16 +20,11 @@ class FcmReceiver {
|
|||||||
static const int MAX_COUNT_RETRY_TO_GET_FCM_TOKEN = 3;
|
static const int MAX_COUNT_RETRY_TO_GET_FCM_TOKEN = 3;
|
||||||
|
|
||||||
Future onInitialFcmListener() async {
|
Future onInitialFcmListener() async {
|
||||||
_onForegroundMessage();
|
|
||||||
_onBackgroundMessage();
|
_onBackgroundMessage();
|
||||||
|
|
||||||
await _onHandleFcmToken();
|
await _onHandleFcmToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onForegroundMessage() {
|
|
||||||
FirebaseMessaging.onMessage.listen(FcmService.instance.handleFirebaseForegroundMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _onBackgroundMessage() {
|
void _onBackgroundMessage() {
|
||||||
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
|
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:core/utils/app_logger.dart';
|
import 'package:core/utils/app_logger.dart';
|
||||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/presentation/model/broadcast_message_event_data.dart';
|
|
||||||
import 'package:universal_html/html.dart' as html;
|
|
||||||
|
|
||||||
class FcmService {
|
class FcmService {
|
||||||
|
|
||||||
StreamController<Map<String, dynamic>>? foregroundMessageStreamController;
|
|
||||||
StreamController<Map<String, dynamic>>? backgroundMessageStreamController;
|
StreamController<Map<String, dynamic>>? backgroundMessageStreamController;
|
||||||
StreamController<String?>? fcmTokenStreamController;
|
StreamController<String?>? fcmTokenStreamController;
|
||||||
|
|
||||||
@@ -19,13 +15,6 @@ class FcmService {
|
|||||||
|
|
||||||
static FcmService get instance => _instance;
|
static FcmService get instance => _instance;
|
||||||
|
|
||||||
void handleFirebaseForegroundMessage(RemoteMessage newRemoteMessage) {
|
|
||||||
log('FcmService::handleFirebaseForegroundMessage():data: ${newRemoteMessage.data}');
|
|
||||||
if (newRemoteMessage.data.isNotEmpty) {
|
|
||||||
foregroundMessageStreamController?.add(newRemoteMessage.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleFirebaseBackgroundMessage(RemoteMessage newRemoteMessage) {
|
void handleFirebaseBackgroundMessage(RemoteMessage newRemoteMessage) {
|
||||||
log('FcmService::handleFirebaseBackgroundMessage():data: ${newRemoteMessage.data}');
|
log('FcmService::handleFirebaseBackgroundMessage():data: ${newRemoteMessage.data}');
|
||||||
if (newRemoteMessage.data.isNotEmpty) {
|
if (newRemoteMessage.data.isNotEmpty) {
|
||||||
@@ -33,19 +22,6 @@ class FcmService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleMessageEventBroadcastChannel(html.MessageEvent messageEvent) {
|
|
||||||
log('FcmService::handleMessageEventBroadcastChannel():TYPE: ${messageEvent.data.runtimeType} | DATA: ${messageEvent.data}');
|
|
||||||
try {
|
|
||||||
final jsonEventData = jsonDecode(jsonEncode(messageEvent.data)) as Map<String, dynamic>;
|
|
||||||
final eventData = BroadcastMessageEventData.fromJson(jsonEventData);
|
|
||||||
if (eventData.data?.isNotEmpty == true) {
|
|
||||||
foregroundMessageStreamController?.add(eventData.data!);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
logError('FcmService::handleMessageEventBroadcastChannel: Exception = $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleToken(String? token) {
|
void handleToken(String? token) {
|
||||||
log('FcmService::handleToken():token: $token');
|
log('FcmService::handleToken():token: $token');
|
||||||
fcmTokenStreamController?.add(token);
|
fcmTokenStreamController?.add(token);
|
||||||
@@ -53,17 +29,14 @@ class FcmService {
|
|||||||
|
|
||||||
void initialStreamController() {
|
void initialStreamController() {
|
||||||
log('FcmService::initialStreamController:');
|
log('FcmService::initialStreamController:');
|
||||||
foregroundMessageStreamController = StreamController<Map<String, dynamic>>.broadcast();
|
|
||||||
backgroundMessageStreamController = StreamController<Map<String, dynamic>>.broadcast();
|
backgroundMessageStreamController = StreamController<Map<String, dynamic>>.broadcast();
|
||||||
fcmTokenStreamController = StreamController<String?>.broadcast();
|
fcmTokenStreamController = StreamController<String?>.broadcast();
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeStream() {
|
void closeStream() {
|
||||||
foregroundMessageStreamController?.close();
|
|
||||||
backgroundMessageStreamController?.close();
|
backgroundMessageStreamController?.close();
|
||||||
fcmTokenStreamController?.close();
|
fcmTokenStreamController?.close();
|
||||||
|
|
||||||
foregroundMessageStreamController = null;
|
|
||||||
backgroundMessageStreamController = null;
|
backgroundMessageStreamController = null;
|
||||||
fcmTokenStreamController = null;
|
fcmTokenStreamController = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user