chore: Follow up notification actions
This commit is contained in:
parent
40bd7eed91
commit
a88849c2db
3 changed files with 103 additions and 8 deletions
|
|
@ -89,6 +89,7 @@ class BackgroundPush {
|
||||||
NotificationResponseJson.fromJsonString(message),
|
NotificationResponseJson.fromJsonString(message),
|
||||||
client: client,
|
client: client,
|
||||||
router: FluffyChatApp.router,
|
router: FluffyChatApp.router,
|
||||||
|
l10n: l10n,
|
||||||
);
|
);
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logs().wtf('Main Notification Tap crashed', e, s);
|
Logs().wtf('Main Notification Tap crashed', e, s);
|
||||||
|
|
@ -105,6 +106,7 @@ class BackgroundPush {
|
||||||
response,
|
response,
|
||||||
client: client,
|
client: client,
|
||||||
router: FluffyChatApp.router,
|
router: FluffyChatApp.router,
|
||||||
|
l10n: l10n,
|
||||||
),
|
),
|
||||||
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
|
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
|
||||||
);
|
);
|
||||||
|
|
@ -313,6 +315,7 @@ class BackgroundPush {
|
||||||
response,
|
response,
|
||||||
client: client,
|
client: client,
|
||||||
router: FluffyChatApp.router,
|
router: FluffyChatApp.router,
|
||||||
|
l10n: l10n,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,13 @@ import 'package:go_router/go_router.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
import 'package:fluffychat/l10n/l10n.dart';
|
||||||
import 'package:fluffychat/utils/client_manager.dart';
|
import 'package:fluffychat/utils/client_manager.dart';
|
||||||
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||||
|
import 'package:fluffychat/utils/platform_infos.dart';
|
||||||
|
import 'package:fluffychat/utils/push_helper.dart';
|
||||||
|
import '../config/app_config.dart';
|
||||||
|
import '../config/setting_keys.dart';
|
||||||
|
|
||||||
bool _vodInitialized = false;
|
bool _vodInitialized = false;
|
||||||
|
|
||||||
|
|
@ -52,9 +58,10 @@ void notificationTapBackground(
|
||||||
await vod.init();
|
await vod.init();
|
||||||
_vodInitialized = true;
|
_vodInitialized = true;
|
||||||
}
|
}
|
||||||
|
final store = await SharedPreferences.getInstance();
|
||||||
final client = (await ClientManager.getClients(
|
final client = (await ClientManager.getClients(
|
||||||
initialize: false,
|
initialize: false,
|
||||||
store: await SharedPreferences.getInstance(),
|
store: store,
|
||||||
))
|
))
|
||||||
.first;
|
.first;
|
||||||
await client.abortSync();
|
await client.abortSync();
|
||||||
|
|
@ -62,6 +69,11 @@ void notificationTapBackground(
|
||||||
waitForFirstSync: false,
|
waitForFirstSync: false,
|
||||||
waitUntilLoadCompletedLoaded: false,
|
waitUntilLoadCompletedLoaded: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
AppConfig.sendPublicReadReceipts =
|
||||||
|
store.getBool(SettingKeys.sendPublicReadReceipts) ??
|
||||||
|
AppConfig.sendPublicReadReceipts;
|
||||||
|
|
||||||
if (!client.isLogged()) {
|
if (!client.isLogged()) {
|
||||||
throw Exception('Notification tab in background but not logged in!');
|
throw Exception('Notification tab in background but not logged in!');
|
||||||
}
|
}
|
||||||
|
|
@ -77,14 +89,17 @@ Future<void> notificationTap(
|
||||||
NotificationResponse notificationResponse, {
|
NotificationResponse notificationResponse, {
|
||||||
GoRouter? router,
|
GoRouter? router,
|
||||||
required Client client,
|
required Client client,
|
||||||
|
L10n? l10n,
|
||||||
}) async {
|
}) async {
|
||||||
Logs().d(
|
Logs().d(
|
||||||
'Notification action handler started',
|
'Notification action handler started',
|
||||||
notificationResponse.notificationResponseType.name,
|
notificationResponse.notificationResponseType.name,
|
||||||
);
|
);
|
||||||
|
final payload =
|
||||||
|
FluffyChatPushPayload.fromString(notificationResponse.payload ?? '');
|
||||||
switch (notificationResponse.notificationResponseType) {
|
switch (notificationResponse.notificationResponseType) {
|
||||||
case NotificationResponseType.selectedNotification:
|
case NotificationResponseType.selectedNotification:
|
||||||
final roomId = notificationResponse.payload;
|
final roomId = payload.roomId;
|
||||||
if (roomId == null) return;
|
if (roomId == null) return;
|
||||||
|
|
||||||
if (router == null) {
|
if (router == null) {
|
||||||
|
|
@ -111,7 +126,7 @@ Future<void> notificationTap(
|
||||||
if (actionType == null) {
|
if (actionType == null) {
|
||||||
throw Exception('Selected notification with action but no action ID');
|
throw Exception('Selected notification with action but no action ID');
|
||||||
}
|
}
|
||||||
final roomId = notificationResponse.payload;
|
final roomId = payload.roomId;
|
||||||
if (roomId == null) {
|
if (roomId == null) {
|
||||||
throw Exception('Selected notification with action but no payload');
|
throw Exception('Selected notification with action but no payload');
|
||||||
}
|
}
|
||||||
|
|
@ -127,9 +142,9 @@ Future<void> notificationTap(
|
||||||
switch (actionType) {
|
switch (actionType) {
|
||||||
case FluffyChatNotificationActions.markAsRead:
|
case FluffyChatNotificationActions.markAsRead:
|
||||||
await room.setReadMarker(
|
await room.setReadMarker(
|
||||||
room.lastEvent!.eventId,
|
payload.eventId ?? room.lastEvent!.eventId,
|
||||||
mRead: room.lastEvent!.eventId,
|
mRead: payload.eventId ?? room.lastEvent!.eventId,
|
||||||
public: false, // TODO: Load preference here
|
public: AppConfig.sendPublicReadReceipts,
|
||||||
);
|
);
|
||||||
case FluffyChatNotificationActions.reply:
|
case FluffyChatNotificationActions.reply:
|
||||||
final input = notificationResponse.input;
|
final input = notificationResponse.input;
|
||||||
|
|
@ -138,7 +153,65 @@ Future<void> notificationTap(
|
||||||
'Selected notification with reply action but without input',
|
'Selected notification with reply action but without input',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
await room.sendTextEvent(input);
|
|
||||||
|
final eventId = await room.sendTextEvent(input);
|
||||||
|
|
||||||
|
if (PlatformInfos.isAndroid) {
|
||||||
|
final messagingStyleInformation =
|
||||||
|
await AndroidFlutterLocalNotificationsPlugin()
|
||||||
|
.getActiveNotificationMessagingStyle(room.id.hashCode);
|
||||||
|
if (messagingStyleInformation == null) return;
|
||||||
|
l10n ??= await lookupL10n(const Locale('en'));
|
||||||
|
messagingStyleInformation.messages?.add(
|
||||||
|
Message(
|
||||||
|
input,
|
||||||
|
DateTime.now(),
|
||||||
|
Person(key: room.client.userID, name: l10n.you),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await FlutterLocalNotificationsPlugin().show(
|
||||||
|
room.id.hashCode,
|
||||||
|
room.getLocalizedDisplayname(MatrixLocals(l10n)),
|
||||||
|
input,
|
||||||
|
NotificationDetails(
|
||||||
|
android: AndroidNotificationDetails(
|
||||||
|
AppConfig.pushNotificationsChannelId,
|
||||||
|
l10n.incomingMessages,
|
||||||
|
category: AndroidNotificationCategory.message,
|
||||||
|
shortcutId: room.id,
|
||||||
|
styleInformation: messagingStyleInformation,
|
||||||
|
groupKey: room.id,
|
||||||
|
playSound: false,
|
||||||
|
enableVibration: false,
|
||||||
|
actions: <AndroidNotificationAction>[
|
||||||
|
AndroidNotificationAction(
|
||||||
|
FluffyChatNotificationActions.reply.name,
|
||||||
|
l10n.reply,
|
||||||
|
inputs: [
|
||||||
|
AndroidNotificationActionInput(
|
||||||
|
label: l10n.writeAMessage,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
cancelNotification: false,
|
||||||
|
allowGeneratedReplies: true,
|
||||||
|
semanticAction: SemanticAction.reply,
|
||||||
|
),
|
||||||
|
AndroidNotificationAction(
|
||||||
|
FluffyChatNotificationActions.markAsRead.name,
|
||||||
|
l10n.markAsRead,
|
||||||
|
semanticAction: SemanticAction.markAsRead,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
payload: FluffyChatPushPayload(
|
||||||
|
client.clientName,
|
||||||
|
room.id,
|
||||||
|
eventId,
|
||||||
|
).toString(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -315,11 +315,30 @@ Future<void> _tryPushHelper(
|
||||||
title,
|
title,
|
||||||
body,
|
body,
|
||||||
platformChannelSpecifics,
|
platformChannelSpecifics,
|
||||||
payload: event.roomId,
|
payload:
|
||||||
|
FluffyChatPushPayload(client.clientName, event.room.id, event.eventId)
|
||||||
|
.toString(),
|
||||||
);
|
);
|
||||||
Logs().v('Push helper has been completed!');
|
Logs().v('Push helper has been completed!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FluffyChatPushPayload {
|
||||||
|
final String? clientName, roomId, eventId;
|
||||||
|
|
||||||
|
FluffyChatPushPayload(this.clientName, this.roomId, this.eventId);
|
||||||
|
|
||||||
|
factory FluffyChatPushPayload.fromString(String payload) {
|
||||||
|
final parts = payload.split('|');
|
||||||
|
if (parts.length != 3) {
|
||||||
|
return FluffyChatPushPayload(null, null, null);
|
||||||
|
}
|
||||||
|
return FluffyChatPushPayload(parts[0], parts[1], parts[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => '$clientName|$roomId|$eventId';
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a shortcut for Android platform but does not block displaying the
|
/// Creates a shortcut for Android platform but does not block displaying the
|
||||||
/// notification. This is optional but provides a nicer view of the
|
/// notification. This is optional but provides a nicer view of the
|
||||||
/// notification popup.
|
/// notification popup.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue