Skip to content

Commit

Permalink
TW-1907: Fix jump exactly to message in the notification on Mobile (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nqhhdev authored Jul 15, 2024
1 parent 778311b commit 827e0a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
44 changes: 35 additions & 9 deletions lib/utils/background_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <String, Completer<void>>{};

final dynamic fcmSharedIsolate = FcmSharedIsolate();
Expand Down Expand Up @@ -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) {
Expand All @@ -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<void> onSelectNotification(NotificationResponse? response) {
return goToRoom(response?.payload);
Future<void> onSelectNotification(
NotificationResponse? response, {
String? eventId,
}) {
Logs().d(
'BackgroundPush::onSelectNotification() roomId - ${response?.payload} ||eventId - $eventId',
);
return goToRoom(response?.payload, eventId: eventId);
}

Future<void> goToRoom(String? roomId) async {
Future<void> goToRoom(
String? roomId, {
String? eventId,
}) async {
try {
Logs().v('[Push] Attempting to go to room $roomId...');
_clearAllNavigatorAvailable(roomId: roomId);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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');
}
}
2 changes: 2 additions & 0 deletions lib/widgets/local_notifications_extension.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -68,6 +69,7 @@ extension LocalNotificationsExtension on MatrixState {
body,
icon,
eventUpdate.roomID,
eventUpdate.eventId,
]);
} else if (Platform.isLinux) {
final appIconUrl = room.avatar?.getThumbnail(
Expand Down
4 changes: 2 additions & 2 deletions web/script.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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();
Expand Down

0 comments on commit 827e0a1

Please sign in to comment.