From ff3af007e0f4144fe3f78d45f4551c6525bddfaa Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 25 Dec 2023 12:21:55 +0700 Subject: [PATCH] TF-2384 Handle open email detailed when click notification on terminated mode Signed-off-by: dab246 (cherry picked from commit d59ed90fa89755f476150eaee7527282ef0bae7a) --- ios/Runner/AppDelegate.swift | 12 +++++++++++ .../home/presentation/home_controller.dart | 20 +++++++++++++++++-- .../presentation/services/fcm_receiver.dart | 16 ++++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 37dc26dac..440c53a4f 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -8,6 +8,7 @@ import flutter_local_notifications @objc class AppDelegate: FlutterAppDelegate { var notificationInteractionChannel: FlutterMethodChannel? + var initialNotificationInfo: Any? override func application( _ application: UIApplication, @@ -16,6 +17,7 @@ import flutter_local_notifications GeneratedPluginRegistrant.register(with: self) createNotificationInteractionChannel() + initialNotificationInfo = launchOptions?[.remoteNotification] if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate @@ -142,5 +144,15 @@ extension AppDelegate { name: "notification_interaction_channel", binaryMessenger: controller.binaryMessenger ) + + self.notificationInteractionChannel?.setMethodCallHandler { (call, result) in + switch call.method { + case "getInitialNotificationInfo": + result(self.initialNotificationInfo) + self.initialNotificationInfo = nil + default: + break + } + } } } diff --git a/lib/features/home/presentation/home_controller.dart b/lib/features/home/presentation/home_controller.dart index 663a66bdd..cc7d20bd9 100644 --- a/lib/features/home/presentation/home_controller.dart +++ b/lib/features/home/presentation/home_controller.dart @@ -2,6 +2,7 @@ import 'package:core/utils/platform_info.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_downloader/flutter_downloader.dart'; import 'package:get/get.dart'; +import 'package:jmap_dart_client/jmap/core/id.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; import 'package:jmap_dart_client/jmap/mail/email/email.dart'; import 'package:jmap_dart_client/jmap/mail/email/email_address.dart'; @@ -21,6 +22,7 @@ import 'package:tmail_ui_user/features/cleanup/domain/usecases/cleanup_recent_lo import 'package:tmail_ui_user/features/cleanup/domain/usecases/cleanup_recent_login_username_interactor.dart'; import 'package:tmail_ui_user/features/cleanup/domain/usecases/cleanup_recent_search_cache_interactor.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/preview_email_arguments.dart'; +import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_receiver.dart'; import 'package:tmail_ui_user/main/routes/app_routes.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; import 'package:tmail_ui_user/main/routes/route_utils.dart'; @@ -50,8 +52,8 @@ class HomeController extends ReloadableController { _initFlutterDownloader(); _registerReceivingSharingIntent(); } - if (PlatformInfo.isIOS && Get.arguments is EmailId) { - _emailIdPreview = Get.arguments; + if (PlatformInfo.isIOS) { + _handleIOSDataMessage(); } super.onInit(); } @@ -114,4 +116,18 @@ class HomeController extends ReloadableController { _emailReceiveManager.receivingFileSharingStream.listen(_emailReceiveManager.setPendingFileInfo); } + + Future _handleIOSDataMessage() async { + if (Get.arguments is EmailId) { + _emailIdPreview = Get.arguments; + } else { + final notificationInfo = await FcmReceiver.instance.getIOSInitialNotificationInfo(); + if (notificationInfo != null && notificationInfo.containsKey('email_id')) { + final emailId = notificationInfo['email_id'] as String?; + if (emailId?.isNotEmpty == true) { + _emailIdPreview = EmailId(Id(emailId!)); + } + } + } + } } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index ff7894bd4..512b7a1ed 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -33,7 +33,7 @@ class FcmReceiver { if (PlatformInfo.isIOS) { notificationInteractionChannel.setMethodCallHandler((call) async { log('FcmReceiver::onInitialFcmListener:notificationInteractionChannel: $call'); - if (call.method == 'openEmail') { + if (call.method == 'openEmail' && call.arguments is String) { log('FcmReceiver::onInitialFcmListener:openEmail with id = ${call.arguments}'); FcmService.instance.handleOpenEmailFromNotification(call.arguments); } @@ -72,4 +72,18 @@ class FcmReceiver { Future deleteFcmToken() async { await FirebaseMessaging.instance.deleteToken(); } + + Future?> getIOSInitialNotificationInfo() async { + try { + final notificationInfo = await notificationInteractionChannel.invokeMethod('getInitialNotificationInfo'); + log('FcmReceiver::getIOSInitialNotificationInfo:notificationInfo: $notificationInfo'); + if (notificationInfo != null && notificationInfo is Map) { + return notificationInfo; + } + return null; + } catch (e) { + logError('FcmReceiver::getIOSInitialNotificationInfo: Exception: $e'); + return null; + } + } } \ No newline at end of file