2022-01-22 11:16:40 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
2022-02-18 20:33:36 +01:00
|
|
|
import 'package:desktop_lifecycle/desktop_lifecycle.dart';
|
2022-01-22 11:16:40 +01:00
|
|
|
import 'package:desktop_notifications/desktop_notifications.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2022-01-28 17:53:13 +01:00
|
|
|
import 'package:http/http.dart' as http;
|
2022-01-22 11:16:40 +01:00
|
|
|
import 'package:matrix/matrix.dart';
|
2022-01-28 17:53:13 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-01-22 11:16:40 +01:00
|
|
|
import 'package:universal_html/html.dart' as html;
|
2022-02-03 07:44:50 +01:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2022-01-22 11:16:40 +01:00
|
|
|
|
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
|
|
|
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/matrix_locals.dart';
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2022-01-22 11:16:40 +01:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
|
|
|
|
extension LocalNotificationsExtension on MatrixState {
|
|
|
|
void showLocalNotification(EventUpdate eventUpdate) async {
|
|
|
|
final roomId = eventUpdate.roomID;
|
2022-02-18 20:33:36 +01:00
|
|
|
if (activeRoomId == roomId) {
|
|
|
|
if (kIsWeb && webHasFocus) return;
|
2022-09-10 12:12:52 +02:00
|
|
|
if (PlatformInfos.isLinux && DesktopLifecycle.instance.isActive.value) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-18 20:33:36 +01:00
|
|
|
}
|
2022-01-22 11:16:40 +01:00
|
|
|
final room = client.getRoomById(roomId);
|
2022-01-28 18:21:20 +01:00
|
|
|
if (room == null) {
|
|
|
|
Logs().w('Can not display notification for unknown room $roomId');
|
|
|
|
return;
|
|
|
|
}
|
2022-01-22 11:16:40 +01:00
|
|
|
if (room.notificationCount == 0) return;
|
|
|
|
final event = Event.fromJson(eventUpdate.content, room);
|
|
|
|
final title =
|
2022-01-28 18:21:20 +01:00
|
|
|
room.getLocalizedDisplayname(MatrixLocals(L10n.of(widget.context)!));
|
2022-05-30 13:44:05 +02:00
|
|
|
final body = await event.calcLocalizedBody(
|
2022-01-28 18:21:20 +01:00
|
|
|
MatrixLocals(L10n.of(widget.context)!),
|
2022-01-22 11:16:40 +01:00
|
|
|
withSenderNamePrefix:
|
2022-01-28 18:21:20 +01:00
|
|
|
!room.isDirectChat || room.lastEvent?.senderId == client.userID,
|
2022-01-22 11:16:40 +01:00
|
|
|
plaintextBody: true,
|
|
|
|
hideReply: true,
|
|
|
|
hideEdit: true,
|
2022-04-13 16:02:47 +02:00
|
|
|
removeMarkdown: true,
|
2022-01-22 11:16:40 +01:00
|
|
|
);
|
2022-05-30 13:44:05 +02:00
|
|
|
final icon = event.senderFromMemoryOrFallback.avatarUrl?.getThumbnail(
|
|
|
|
client,
|
|
|
|
width: 64,
|
|
|
|
height: 64,
|
|
|
|
method: ThumbnailMethod.crop) ??
|
2022-01-22 11:16:40 +01:00
|
|
|
room.avatar?.getThumbnail(client,
|
|
|
|
width: 64, height: 64, method: ThumbnailMethod.crop);
|
|
|
|
if (kIsWeb) {
|
|
|
|
html.AudioElement()
|
2022-01-22 11:20:56 +01:00
|
|
|
..src =
|
|
|
|
'assets/assets/sounds/WoodenBeaver_stereo_message-new-instant.ogg'
|
2022-01-22 11:16:40 +01:00
|
|
|
..autoplay = true
|
|
|
|
..load();
|
|
|
|
html.Notification(
|
|
|
|
title,
|
|
|
|
body: body,
|
|
|
|
icon: icon.toString(),
|
|
|
|
);
|
|
|
|
} else if (Platform.isLinux) {
|
2022-01-28 17:53:13 +01:00
|
|
|
final appIconUrl = room.avatar?.getThumbnail(
|
|
|
|
room.client,
|
|
|
|
width: 56,
|
|
|
|
height: 56,
|
|
|
|
);
|
2022-01-28 18:21:20 +01:00
|
|
|
File? appIconFile;
|
2022-01-28 17:53:13 +01:00
|
|
|
if (appIconUrl != null) {
|
|
|
|
final tempDirectory = await getApplicationSupportDirectory();
|
|
|
|
final avatarDirectory =
|
|
|
|
await Directory('${tempDirectory.path}/notiavatars/').create();
|
|
|
|
appIconFile = File(
|
|
|
|
'${avatarDirectory.path}/${Uri.encodeComponent(appIconUrl.toString())}');
|
|
|
|
if (await appIconFile.exists() == false) {
|
|
|
|
final response = await http.get(appIconUrl);
|
|
|
|
await appIconFile.writeAsBytes(response.bodyBytes);
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
final notification = await linuxNotifications!.notify(
|
2022-01-22 11:16:40 +01:00
|
|
|
title,
|
|
|
|
body: body,
|
|
|
|
replacesId: linuxNotificationIds[roomId] ?? 0,
|
|
|
|
appName: AppConfig.applicationName,
|
2022-01-28 18:21:20 +01:00
|
|
|
appIcon: appIconFile?.path ?? '',
|
2022-01-22 11:16:40 +01:00
|
|
|
actions: [
|
|
|
|
NotificationAction(
|
2022-02-03 07:44:50 +01:00
|
|
|
DesktopNotificationActions.openChat.name,
|
|
|
|
L10n.of(widget.context)!.openChat,
|
2022-01-22 11:16:40 +01:00
|
|
|
),
|
|
|
|
NotificationAction(
|
|
|
|
DesktopNotificationActions.seen.name,
|
2022-01-28 18:21:20 +01:00
|
|
|
L10n.of(widget.context)!.markAsRead,
|
2022-01-22 11:16:40 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
hints: [
|
|
|
|
NotificationHint.soundName('message-new-instant'),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
notification.action.then((actionStr) {
|
|
|
|
final action = DesktopNotificationActions.values
|
|
|
|
.singleWhere((a) => a.name == actionStr);
|
|
|
|
switch (action) {
|
|
|
|
case DesktopNotificationActions.seen:
|
|
|
|
room.setReadMarker(event.eventId, mRead: event.eventId);
|
|
|
|
break;
|
2022-02-03 07:44:50 +01:00
|
|
|
case DesktopNotificationActions.openChat:
|
|
|
|
VRouter.of(navigatorContext).toSegments(['rooms', room.id]);
|
2022-01-22 11:16:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
linuxNotificationIds[roomId] = notification.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 07:44:50 +01:00
|
|
|
enum DesktopNotificationActions { seen, openChat }
|