feat: Add notification actions
This commit is contained in:
parent
2e07b7bcf1
commit
04b5ecce54
4 changed files with 102 additions and 8 deletions
|
|
@ -20,6 +20,8 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
@ -73,6 +75,27 @@ class BackgroundPush {
|
||||||
|
|
||||||
void _init() async {
|
void _init() async {
|
||||||
try {
|
try {
|
||||||
|
if (PlatformInfos.isAndroid) {
|
||||||
|
final port = ReceivePort();
|
||||||
|
IsolateNameServer.removePortNameMapping('background_tab_port');
|
||||||
|
IsolateNameServer.registerPortWithName(
|
||||||
|
port.sendPort,
|
||||||
|
'background_tab_port',
|
||||||
|
);
|
||||||
|
port.listen(
|
||||||
|
(message) async {
|
||||||
|
try {
|
||||||
|
await notificationTap(
|
||||||
|
NotificationResponseJson.fromJsonString(message),
|
||||||
|
client: client,
|
||||||
|
router: FluffyChatApp.router,
|
||||||
|
);
|
||||||
|
} catch (e, s) {
|
||||||
|
Logs().wtf('Main Notification Tap crashed', e, s);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
await _flutterLocalNotificationsPlugin.initialize(
|
await _flutterLocalNotificationsPlugin.initialize(
|
||||||
const InitializationSettings(
|
const InitializationSettings(
|
||||||
android: AndroidInitializationSettings('notifications_icon'),
|
android: AndroidInitializationSettings('notifications_icon'),
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,7 @@ Future<MatrixSdkDatabase> _constructDatabase(String clientName) async {
|
||||||
version: 1,
|
version: 1,
|
||||||
// most important : apply encryption when opening the DB
|
// most important : apply encryption when opening the DB
|
||||||
onConfigure: helper?.applyPragmaKey,
|
onConfigure: helper?.applyPragmaKey,
|
||||||
|
singleInstance: false,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,87 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
import 'package:flutter_vodozemac/flutter_vodozemac.dart' as vod;
|
||||||
import 'package:go_router/go_router.dart';
|
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/utils/client_manager.dart';
|
import 'package:fluffychat/utils/client_manager.dart';
|
||||||
|
|
||||||
|
bool _vodInitialized = false;
|
||||||
|
|
||||||
|
extension NotificationResponseJson on NotificationResponse {
|
||||||
|
String toJsonString() => jsonEncode({
|
||||||
|
'type': notificationResponseType.name,
|
||||||
|
'id': id,
|
||||||
|
'actionId': actionId,
|
||||||
|
'input': input,
|
||||||
|
'payload': payload,
|
||||||
|
'data': data,
|
||||||
|
});
|
||||||
|
|
||||||
|
static NotificationResponse fromJsonString(String jsonString) {
|
||||||
|
final json = jsonDecode(jsonString) as Map<String, Object?>;
|
||||||
|
return NotificationResponse(
|
||||||
|
notificationResponseType: NotificationResponseType.values
|
||||||
|
.singleWhere((t) => t.name == json['type']),
|
||||||
|
id: json['id'] as int?,
|
||||||
|
actionId: json['actionId'] as String?,
|
||||||
|
input: json['input'] as String?,
|
||||||
|
payload: json['payload'] as String?,
|
||||||
|
data: json['data'] as Map<String, dynamic>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@pragma('vm:entry-point')
|
@pragma('vm:entry-point')
|
||||||
void notificationTapBackground(
|
void notificationTapBackground(
|
||||||
NotificationResponse notificationResponse,
|
NotificationResponse notificationResponse,
|
||||||
) async {
|
) async {
|
||||||
|
Logs().i('Notification tap in background');
|
||||||
|
|
||||||
|
final sendPort = IsolateNameServer.lookupPortByName('background_tab_port');
|
||||||
|
if (sendPort != null) {
|
||||||
|
sendPort.send(notificationResponse.toJsonString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_vodInitialized) {
|
||||||
|
await vod.init();
|
||||||
|
_vodInitialized = true;
|
||||||
|
}
|
||||||
final client = (await ClientManager.getClients(
|
final client = (await ClientManager.getClients(
|
||||||
initialize: false,
|
initialize: false,
|
||||||
store: await SharedPreferences.getInstance(),
|
store: await SharedPreferences.getInstance(),
|
||||||
))
|
))
|
||||||
.first;
|
.first;
|
||||||
notificationTap(notificationResponse, client: client);
|
await client.abortSync();
|
||||||
|
await client.init(
|
||||||
|
waitForFirstSync: false,
|
||||||
|
waitUntilLoadCompletedLoaded: false,
|
||||||
|
);
|
||||||
|
if (!client.isLogged()) {
|
||||||
|
throw Exception('Notification tab in background but not logged in!');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await notificationTap(notificationResponse, client: client);
|
||||||
|
} finally {
|
||||||
|
await client.dispose(closeDatabase: false);
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void notificationTap(
|
Future<void> notificationTap(
|
||||||
NotificationResponse notificationResponse, {
|
NotificationResponse notificationResponse, {
|
||||||
GoRouter? router,
|
GoRouter? router,
|
||||||
required Client client,
|
required Client client,
|
||||||
}) async {
|
}) async {
|
||||||
|
Logs().d(
|
||||||
|
'Notification action handler started',
|
||||||
|
notificationResponse.notificationResponseType.name,
|
||||||
|
);
|
||||||
switch (notificationResponse.notificationResponseType) {
|
switch (notificationResponse.notificationResponseType) {
|
||||||
case NotificationResponseType.selectedNotification:
|
case NotificationResponseType.selectedNotification:
|
||||||
final roomId = notificationResponse.payload;
|
final roomId = notificationResponse.payload;
|
||||||
|
|
@ -56,6 +115,9 @@ void notificationTap(
|
||||||
if (roomId == null) {
|
if (roomId == null) {
|
||||||
throw Exception('Selected notification with action but no payload');
|
throw Exception('Selected notification with action but no payload');
|
||||||
}
|
}
|
||||||
|
await client.roomsLoading;
|
||||||
|
await client.accountDataLoading;
|
||||||
|
await client.userDeviceKeysLoading;
|
||||||
final room = client.getRoomById(roomId);
|
final room = client.getRoomById(roomId);
|
||||||
if (room == null) {
|
if (room == null) {
|
||||||
throw Exception(
|
throw Exception(
|
||||||
|
|
@ -64,7 +126,11 @@ void notificationTap(
|
||||||
}
|
}
|
||||||
switch (actionType) {
|
switch (actionType) {
|
||||||
case FluffyChatNotificationActions.markAsRead:
|
case FluffyChatNotificationActions.markAsRead:
|
||||||
await room.setReadMarker(room.lastEvent!.eventId);
|
await room.setReadMarker(
|
||||||
|
room.lastEvent!.eventId,
|
||||||
|
mRead: room.lastEvent!.eventId,
|
||||||
|
public: false, // TODO: Load preference here
|
||||||
|
);
|
||||||
case FluffyChatNotificationActions.reply:
|
case FluffyChatNotificationActions.reply:
|
||||||
final input = notificationResponse.input;
|
final input = notificationResponse.input;
|
||||||
if (input == null || input.isEmpty) {
|
if (input == null || input.isEmpty) {
|
||||||
|
|
|
||||||
|
|
@ -279,16 +279,20 @@ Future<void> _tryPushHelper(
|
||||||
priority: Priority.max,
|
priority: Priority.max,
|
||||||
groupKey: event.room.spaceParents.firstOrNull?.roomId ?? 'rooms',
|
groupKey: event.room.spaceParents.firstOrNull?.roomId ?? 'rooms',
|
||||||
actions: <AndroidNotificationAction>[
|
actions: <AndroidNotificationAction>[
|
||||||
AndroidNotificationAction(
|
|
||||||
FluffyChatNotificationActions.markAsRead.name,
|
|
||||||
l10n.markAsRead,
|
|
||||||
),
|
|
||||||
AndroidNotificationAction(
|
AndroidNotificationAction(
|
||||||
FluffyChatNotificationActions.reply.name,
|
FluffyChatNotificationActions.reply.name,
|
||||||
l10n.reply,
|
l10n.reply,
|
||||||
inputs: [
|
inputs: [
|
||||||
const AndroidNotificationActionInput(),
|
AndroidNotificationActionInput(
|
||||||
|
label: l10n.writeAMessage,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
|
cancelNotification: false,
|
||||||
|
allowGeneratedReplies: true,
|
||||||
|
),
|
||||||
|
AndroidNotificationAction(
|
||||||
|
FluffyChatNotificationActions.markAsRead.name,
|
||||||
|
l10n.markAsRead,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue