From 827e0a1444e25f231a5a6ed88b6a9232cef0264d Mon Sep 17 00:00:00 2001 From: Quang Huy Nguyen Date: Mon, 15 Jul 2024 15:07:20 +0700 Subject: [PATCH] TW-1907: Fix jump exactly to message in the notification on Mobile (#1948) * TW-1907: Fix jump exactly to message in the notification on Mobile * TW-1907: Fix jump exactly to message in the notification on Web --- lib/utils/background_push.dart | 44 +++++++++++++++---- .../local_notifications_extension.dart | 2 + web/script.js | 4 +- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 605f502d4e..e91a0e454c 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -68,6 +68,9 @@ class BackgroundPush { (await L10n.delegate.load(View.of(context!).platformDispatcher.locale)); } + bool isCurrentRoomActive(String? roomId) => + roomId != null && TwakeApp.router.activeRoomId?.contains(roomId) == true; + final pendingTests = >{}; final dynamic fcmSharedIsolate = FcmSharedIsolate(); @@ -346,7 +349,8 @@ class BackgroundPush { void iOSUserSelectedNoti(dynamic noti) { // roomId is payload if noti is local final roomId = noti['room_id'] ?? noti['payload']; - goToRoom(roomId); + final eventId = noti['event_id']; + goToRoom(roomId, eventId: eventId); } void onReceiveNotification(dynamic message) { @@ -359,16 +363,28 @@ class BackgroundPush { client: client, l10n: l10n, activeRoomId: _matrixState?.activeRoomId, - onSelectNotification: onSelectNotification, + onSelectNotification: (data) => onSelectNotification( + data, + eventId: notification.eventId, + ), ); Logs().d('BackgroundPush::onMessage(): finished pushHelper'); } - Future onSelectNotification(NotificationResponse? response) { - return goToRoom(response?.payload); + Future onSelectNotification( + NotificationResponse? response, { + String? eventId, + }) { + Logs().d( + 'BackgroundPush::onSelectNotification() roomId - ${response?.payload} ||eventId - $eventId', + ); + return goToRoom(response?.payload, eventId: eventId); } - Future goToRoom(String? roomId) async { + Future goToRoom( + String? roomId, { + String? eventId, + }) async { try { Logs().v('[Push] Attempting to go to room $roomId...'); _clearAllNavigatorAvailable(roomId: roomId); @@ -388,7 +404,7 @@ class BackgroundPush { Logs().v('[Push] Room $roomId not found, syncing...'); await client.waitForRoomInSync(roomId); } - TwakeApp.router.go('/rooms/$roomId'); + _handleRedirectRoom(roomId, eventId: eventId); } catch (e, s) { Logs().e('[Push] Failed to open room', e, s); } @@ -614,10 +630,9 @@ class BackgroundPush { String? roomId, }) { Logs().d( - "BackgroundPush:: - Current active room id @2 ${TwakeApp.router.activeRoomId}", + "BackgroundPush:: - Current active room id ${TwakeApp.router.activeRoomId}", ); - if (roomId != null && - TwakeApp.router.activeRoomId?.contains(roomId) == true) { + if (isCurrentRoomActive(roomId)) { return; } @@ -627,4 +642,15 @@ class BackgroundPush { TwakeApp.router.routerDelegate.pop(); } } + + void _handleRedirectRoom( + String roomId, { + String? eventId, + }) { + if (eventId != null) { + TwakeApp.router.go('/rooms/$roomId?event=$eventId'); + return; + } + TwakeApp.router.go('/rooms/$roomId'); + } } diff --git a/lib/widgets/local_notifications_extension.dart b/lib/widgets/local_notifications_extension.dart index b688b89cdc..d0cfc92457 100644 --- a/lib/widgets/local_notifications_extension.dart +++ b/lib/widgets/local_notifications_extension.dart @@ -1,5 +1,6 @@ import 'dart:io'; import 'package:fluffychat/domain/model/room/room_extension.dart'; +import 'package:fluffychat/presentation/extensions/event_update_extension.dart'; import 'package:flutter/foundation.dart'; import 'package:desktop_lifecycle/desktop_lifecycle.dart'; @@ -68,6 +69,7 @@ extension LocalNotificationsExtension on MatrixState { body, icon, eventUpdate.roomID, + eventUpdate.eventId, ]); } else if (Platform.isLinux) { final appIconUrl = room.avatar?.getThumbnail( diff --git a/web/script.js b/web/script.js index 2c066568a6..0aef03ca7a 100644 --- a/web/script.js +++ b/web/script.js @@ -1,4 +1,4 @@ -function handleNotifications(title, body, icon, roomid) { +function handleNotifications(title, body, icon, roomid, eventId) { if ('Notification' in window) { Notification.requestPermission().then(function(permission) { if (permission === 'granted') { @@ -11,7 +11,7 @@ function handleNotifications(title, body, icon, roomid) { console.log('JsFunction::handleNotifications(): On click notification'); var hrefSplit = window.location.href.split('/'); var indexRooms = hrefSplit.indexOf('rooms'); - var redirectURL = hrefSplit.splice(0, indexRooms + 1).join('/') + '/' + roomid; + var redirectURL = hrefSplit.splice(0, indexRooms + 1).join('/') + '/' + roomid + (eventId ? '?event=' + eventId : '') window.parent.parent.focus(); window.location.href = redirectURL; notification.close();