-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Notification actions on android
- Loading branch information
1 parent
2c2e6d0
commit 1817b29
Showing
3 changed files
with
104 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:matrix/matrix.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
import 'package:fluffychat/utils/client_manager.dart'; | ||
|
||
@pragma('vm:entry-point') | ||
void notificationTapBackground( | ||
NotificationResponse notificationResponse, | ||
) async { | ||
final client = (await ClientManager.getClients( | ||
initialize: false, | ||
store: await SharedPreferences.getInstance(), | ||
)) | ||
.first; | ||
notificationTap(notificationResponse, client: client); | ||
} | ||
|
||
void notificationTap( | ||
NotificationResponse notificationResponse, { | ||
GoRouter? router, | ||
required Client client, | ||
}) async { | ||
switch (notificationResponse.notificationResponseType) { | ||
case NotificationResponseType.selectedNotification: | ||
final roomId = notificationResponse.payload; | ||
if (roomId == null) return; | ||
|
||
if (router == null) { | ||
Logs().v('Ignore select notification action in background mode'); | ||
return; | ||
} | ||
Logs().v('Open room from notification tap', roomId); | ||
await client.roomsLoading; | ||
await client.accountDataLoading; | ||
router.go( | ||
client.getRoomById(roomId)?.membership == Membership.invite | ||
? '/rooms' | ||
: '/rooms/$roomId', | ||
); | ||
case NotificationResponseType.selectedNotificationAction: | ||
final actionType = FluffyChatNotificationActions.values.singleWhereOrNull( | ||
(action) => action.name == notificationResponse.actionId, | ||
); | ||
if (actionType == null) { | ||
throw Exception('Selected notification with action but no action ID'); | ||
} | ||
final roomId = notificationResponse.payload; | ||
if (roomId == null) { | ||
throw Exception('Selected notification with action but no payload'); | ||
} | ||
final room = client.getRoomById(roomId); | ||
if (room == null) { | ||
throw Exception( | ||
'Selected notification with action but unknown room $roomId', | ||
); | ||
} | ||
switch (actionType) { | ||
case FluffyChatNotificationActions.markAsRead: | ||
await room.setReadMarker(room.lastEvent!.eventId); | ||
case FluffyChatNotificationActions.reply: | ||
final input = notificationResponse.input; | ||
if (input == null || input.isEmpty) { | ||
throw Exception( | ||
'Selected notification with reply action but without input', | ||
); | ||
} | ||
await room.sendTextEvent(input); | ||
} | ||
} | ||
} | ||
|
||
enum FluffyChatNotificationActions { markAsRead, reply } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters