Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1887: improve notification permission request on web #1954

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,16 @@ class ChatController extends State<Chat>
}
}

void requestNotificationPermission() {
if (PlatformInfos.isWeb) {
try {
sherlockvn marked this conversation as resolved.
Show resolved Hide resolved
html.Notification.requestPermission();
} catch (e) {
Logs().e("Error requesting notification permission: $e");
}
}
}

@override
void dispose() {
unregisterPasteShortcutListeners();
Expand Down Expand Up @@ -2015,7 +2025,10 @@ class ChatController extends State<Chat>
Widget build(BuildContext context) {
return MouseRegion(
onHover: (_) => _resetLocationPath(),
child: ChatView(this, key: widget.key),
child: GestureDetector(
onTap: requestNotificationPermission,
Te-Z marked this conversation as resolved.
Show resolved Hide resolved
child: ChatView(this, key: widget.key),
),
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/pages/chat_list/chat_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:fluffychat/pages/chat_list/chat_list_item_subtitle.dart';
import 'package:fluffychat/pages/chat_list/chat_list_item_title.dart';
import 'package:fluffychat/utils/dialog/twake_dialog.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/twake_snackbar.dart';
import 'package:fluffychat/widgets/avatar/avatar.dart';
import 'package:flutter/material.dart';
Expand All @@ -14,6 +15,7 @@ import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:go_router/go_router.dart';
import 'package:linagora_design_flutter/linagora_design_flutter.dart';
import 'package:matrix/matrix.dart';
import 'package:universal_html/html.dart' as html hide File;

enum ArchivedRoomAction { delete, rejoin }

Expand Down Expand Up @@ -42,6 +44,14 @@ class ChatListItem extends StatelessWidget with ChatListItemMixin {
});

void clickAction(BuildContext context) async {
if (PlatformInfos.isWeb) {
try {
html.Notification.requestPermission();
} catch (e) {
Logs().e("Error requesting notification permission: $e");
}
}

if (onTap != null) return onTap!();
switch (room.membership) {
case Membership.ban:
Expand Down
25 changes: 16 additions & 9 deletions lib/widgets/local_notifications_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -63,14 +62,22 @@ extension LocalNotificationsExtension on MatrixState {
method: ThumbnailMethod.crop,
);
if (kIsWeb) {
_audioPlayer.play();
js.context.callMethod("handleNotifications", [
title,
body,
icon,
eventUpdate.roomID,
eventUpdate.eventId,
]);
try {
await _audioPlayer.play();
} catch (e) {
Logs().e('Can not play notification sound on this browser');
}

try {
js.context.callMethod("handleNotifications", [
title,
body,
icon,
eventUpdate.roomID,
]);
} catch (e) {
Logs().e('Can not display notification on this browser: $e');
}
} else if (Platform.isLinux) {
final appIconUrl = room.avatar?.getThumbnail(
room.client,
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ class MatrixState extends State<Matrix>
currentClient.onUiaRequest.stream.listen(uiaRequestHandler);
if (PlatformInfos.isWeb || PlatformInfos.isLinux) {
currentClient.onSync.stream.first.then((s) {
html.Notification.requestPermission();
onNotification[name] ??= currentClient.onEvent.stream
.where(
(e) =>
Expand Down