fluffychat/lib/utils/push_helper.dart

228 lines
7.2 KiB
Dart
Raw Normal View History

2022-04-14 07:34:55 +02:00
import 'dart:convert';
2022-07-30 13:23:13 +02:00
import 'dart:io';
2022-04-14 07:34:55 +02:00
import 'dart:ui';
2022-04-14 19:26:20 +02:00
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
2022-04-14 07:34:55 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:matrix/matrix.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/setting_keys.dart';
import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
2022-04-14 07:34:55 +02:00
Future<void> pushHelper(
PushNotification notification, {
Client? client,
L10n? l10n,
String? activeRoomId,
Future<dynamic> Function(String?)? onSelectNotification,
2022-07-30 12:49:51 +02:00
}) async {
try {
await _tryPushHelper(
notification,
client: client,
l10n: l10n,
activeRoomId: activeRoomId,
onSelectNotification: onSelectNotification,
);
} catch (e, s) {
Logs().wtf('Push Helper has crashed!', e, s);
// Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
2022-08-14 16:59:21 +02:00
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
2022-07-30 12:49:51 +02:00
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
iOS: IOSInitializationSettings(),
),
onSelectNotification: onSelectNotification,
);
2022-08-14 16:59:21 +02:00
flutterLocalNotificationsPlugin.show(
2022-07-30 12:49:51 +02:00
0,
l10n?.newMessageInFluffyChat,
l10n?.openAppToReadMessages,
2022-07-30 13:23:13 +02:00
NotificationDetails(
iOS: const IOSNotificationDetails(),
android: AndroidNotificationDetails(
AppConfig.pushNotificationsChannelId,
AppConfig.pushNotificationsChannelName,
channelDescription: AppConfig.pushNotificationsChannelDescription,
number: notification.counts?.unread,
ticker: l10n!.unreadChats(notification.counts?.unread ?? 1),
importance: Importance.max,
priority: Priority.high,
)),
2022-07-30 12:49:51 +02:00
);
rethrow;
}
}
Future<void> _tryPushHelper(
PushNotification notification, {
Client? client,
L10n? l10n,
String? activeRoomId,
Future<dynamic> Function(String?)? onSelectNotification,
2022-04-14 07:34:55 +02:00
}) async {
final isBackgroundMessage = client == null;
Logs().v(
'Push helper has been started (background=$isBackgroundMessage).',
notification.toJson(),
);
if (!isBackgroundMessage &&
activeRoomId == notification.roomId &&
activeRoomId != null &&
client?.syncPresence == null) {
Logs().v('Room is in foreground. Stop push helper here.');
return;
}
// Initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
2022-08-14 16:59:21 +02:00
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
2022-04-14 19:26:20 +02:00
const InitializationSettings(
android: AndroidInitializationSettings('notifications_icon'),
iOS: IOSInitializationSettings(),
2022-04-14 07:34:55 +02:00
),
onSelectNotification: onSelectNotification,
);
client ??= (await ClientManager.getClients(initialize: false)).first;
final event = await client.getEventByPushNotification(
notification,
storeInDatabase: isBackgroundMessage,
);
if (event == null) {
Logs().v('Notification is a clearing indicator.');
2022-07-30 12:49:51 +02:00
if (notification.counts?.unread == 0) {
if (notification.counts == null || notification.counts?.unread == 0) {
2022-08-14 16:59:21 +02:00
await flutterLocalNotificationsPlugin.cancelAll();
2022-07-30 12:49:51 +02:00
final store = await SharedPreferences.getInstance();
await store.setString(
SettingKeys.notificationCurrentIds, json.encode({}));
}
}
2022-04-14 07:34:55 +02:00
return;
}
Logs().v('Push helper got notification event.');
if (!event.isEventTypeKnown) {
Logs()
.v('Push message event is from an unknown event type. Do not display.');
return;
}
2022-04-14 07:34:55 +02:00
l10n ??= await L10n.delegate.load(window.locale);
final matrixLocals = MatrixLocals(l10n);
// Calculate the body
final body = await event.calcLocalizedBody(
2022-04-14 07:34:55 +02:00
matrixLocals,
plaintextBody: true,
withSenderNamePrefix: false,
2022-04-14 07:34:55 +02:00
hideReply: true,
hideEdit: true,
removeMarkdown: true,
);
// The person object for the android message style notification
2022-04-15 08:02:23 +02:00
final avatar = event.room.avatar
?.getThumbnail(
client,
width: 126,
height: 126,
)
.toString();
2022-07-30 13:23:13 +02:00
File? avatarFile;
try {
avatarFile = avatar == null
? null
: await DefaultCacheManager().getSingleFile(avatar);
} catch (e, s) {
Logs().e('Unable to get avatar picture', e, s);
}
2022-04-14 19:26:20 +02:00
final id = await mapRoomIdToInt(event.room.id);
2022-04-14 07:34:55 +02:00
// Show notification
final newMessage = Message(
body,
event.originServerTs,
Person(
name: event.senderFromMemoryOrFallback.calcDisplayname(),
icon: avatarFile == null
? null
: BitmapFilePathAndroidIcon(avatarFile.path),
),
);
final messagingStyleInformation = PlatformInfos.isAndroid
? await AndroidFlutterLocalNotificationsPlugin()
.getActiveNotificationMessagingStyle(id)
: null;
messagingStyleInformation?.messages?.add(newMessage);
2022-04-14 07:34:55 +02:00
final androidPlatformChannelSpecifics = AndroidNotificationDetails(
AppConfig.pushNotificationsChannelId,
AppConfig.pushNotificationsChannelName,
2022-04-14 19:26:20 +02:00
channelDescription: AppConfig.pushNotificationsChannelDescription,
number: notification.counts?.unread,
styleInformation: messagingStyleInformation ??
MessagingStyleInformation(
Person(name: event.room.client.userID),
conversationTitle: event.room.displayname,
groupConversation: !event.room.isDirectChat,
messages: [newMessage],
),
2022-04-15 08:02:23 +02:00
ticker: l10n.unreadChats(notification.counts?.unread ?? 1),
2022-04-14 07:34:55 +02:00
importance: Importance.max,
priority: Priority.high,
2022-04-15 08:02:23 +02:00
groupKey: event.room.id,
2022-04-14 07:34:55 +02:00
);
const iOSPlatformChannelSpecifics = IOSNotificationDetails();
final platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
);
2022-08-14 16:59:21 +02:00
await flutterLocalNotificationsPlugin.show(
id,
2022-04-14 07:34:55 +02:00
event.room.displayname,
body,
platformChannelSpecifics,
payload: event.roomId,
);
Logs().v('Push helper has been completed!');
}
/// Workaround for the problem that local notification IDs must be int but we
/// sort by [roomId] which is a String. To make sure that we don't have duplicated
/// IDs we map the [roomId] to a number and store this number.
Future<int> mapRoomIdToInt(String roomId) async {
final store = await SharedPreferences.getInstance();
2022-04-15 08:02:23 +02:00
final idMap = Map<String, int>.from(
jsonDecode(store.getString(SettingKeys.notificationCurrentIds) ?? '{}'));
2022-04-14 07:34:55 +02:00
int? currentInt;
try {
currentInt = idMap[roomId];
} catch (_) {
currentInt = null;
}
if (currentInt != null) {
return currentInt;
}
var nCurrentInt = 0;
2022-04-15 08:02:23 +02:00
while (idMap.values.contains(nCurrentInt)) {
2022-04-14 07:34:55 +02:00
nCurrentInt++;
}
idMap[roomId] = nCurrentInt;
await store.setString(SettingKeys.notificationCurrentIds, json.encode(idMap));
return nCurrentInt;
}