diff --git a/lib/controllers/client.dart b/lib/controllers/client.dart index 3a1ac6c..3ac1d61 100644 --- a/lib/controllers/client.dart +++ b/lib/controllers/client.dart @@ -414,17 +414,17 @@ class ClientController(final String? accountId) extends AsyncNotifier { Future registerPusher(RegisterPusherRequest request) => sendCommand("register_homeserver_push", request.toJson()); - Future deregisterPusher(DeregisterPusherRequest request) => - sendCommand("register_homeserver_push", { - ...request.toJson(), - "kind": null, - }); + Future deregisterPusher(DeregisterPusherRequest request) => sendCommand( + "register_homeserver_push", + {...request.toJson(), "kind": null}, + ); Future uploadMedia(UploadMediaRequest request) async => .fromJson(await sendCommand("upload_media", request.toJson())); Future downloadMedia(DownloadMediaRequest request) async { if (accountId == null && + !isInBackground && ref.read(ClientStateController.provider)?.isLoggedIn != true) { throw StateError("Can't download media while logged out"); } diff --git a/lib/controllers/launch_scope.dart b/lib/controllers/launch_scope.dart new file mode 100644 index 0000000..b4303b8 --- /dev/null +++ b/lib/controllers/launch_scope.dart @@ -0,0 +1,10 @@ +import "package:flutter_riverpod/flutter_riverpod.dart"; + +class LaunchScopeController extends Notifier { + @override + bool build() => true; + + static final provider = NotifierProvider( + LaunchScopeController.new, + ); +} diff --git a/lib/controllers/notification.dart b/lib/controllers/notification.dart index 940ff2a..618d849 100644 --- a/lib/controllers/notification.dart +++ b/lib/controllers/notification.dart @@ -6,6 +6,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:flutter/services.dart"; import "package:material_ui/material_ui.dart"; import "package:nexus/controllers/active_account.dart"; +import "package:nexus/controllers/launch_scope.dart"; import "package:nexus/controllers/portal.dart"; import "package:nexus/controllers/rooms.dart"; import "package:nexus/controllers/sidebar_page.dart"; @@ -63,6 +64,17 @@ class NotificationController AndroidFlutterLocalNotificationsPlugin >() ?.deleteNotificationChannel(channelId: "messages"); + + // Taps that launched the app don't reach the callback above + if (!isInBackground && ref.read(LaunchScopeController.provider)) { + if (await notifications.getNotificationAppLaunchDetails() + case NotificationAppLaunchDetails( + didNotificationLaunchApp: true, + notificationResponse: NotificationResponse(:final payload?), + )) { + _open(payload); + } + } } // Reading a room, here or on another client, dismisses its notifications @@ -95,10 +107,8 @@ class NotificationController static String payloadFor(String accountId, int rowId) => "$accountId:$rowId"; - static const _androidGroup = "nexus.federated.nexus.MESSAGES"; - static const _summaryPayload = "summary"; - static const _messagesChannel = "messages_v2"; + static const _androidGroup = "nexus.federated.nexus.MESSAGES"; /// Dismisses the notifications shown for [threadIds]. Future dismiss(ISet threadIds) async { @@ -131,7 +141,6 @@ class NotificationController navigatorKey.currentState?.popUntil((route) => route.isFirst); ref.read(SidebarPageController.provider.notifier).set(.notifications); - // The summary's payload has no row, so it only opens the notifications final (accountId, rowId) = switch (payload.split(":")) { [final accountId, final rowId] => (accountId, int.tryParse(rowId)), // From before notifications were per account @@ -334,7 +343,7 @@ class NotificationController await controller.show( id: 0, title: "Nexus", - payload: _summaryPayload, + payload: payload, notificationDetails: const .new( android: .new( _messagesChannel, diff --git a/lib/widgets/account_scope.dart b/lib/widgets/account_scope.dart index ff8db09..12badd5 100644 --- a/lib/widgets/account_scope.dart +++ b/lib/widgets/account_scope.dart @@ -3,6 +3,7 @@ import "package:flutter_riverpod/flutter_riverpod.dart"; import "package:flutter_riverpod/misc.dart"; import "package:material_ui/material_ui.dart"; import "package:nexus/controllers/active_account.dart"; +import "package:nexus/controllers/launch_scope.dart"; import "package:nexus/controllers/notification_request.dart"; /// Runs [child] as one account, in a [ProviderScope] of its own that's @@ -23,6 +24,7 @@ class const AccountScope({ accountId: initialAccountId, notificationRowId: null, )); + final isLaunch = useState(true); return ProviderScope( key: ValueKey(active.value.accountId), @@ -30,14 +32,17 @@ class const AccountScope({ observers: observers, overrides: [ ActiveAccountController.provider.overrideWith( - () => ActiveAccountController( - active.value.accountId, - (next) => active.value = next, - ), + () => ActiveAccountController(active.value.accountId, (next) { + isLaunch.value = false; + active.value = next; + }), ), NotificationRequestController.provider.overrideWithBuild( (_, _) => active.value.notificationRowId, ), + LaunchScopeController.provider.overrideWithBuild( + (_, _) => isLaunch.value, + ), ...overrides, ], child: child,