From f4e4b90eabc1e4c9265f771567c45a9a7806c555 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 17 Sep 2020 08:34:36 +0200 Subject: [PATCH] feat: Implement rich push notifications on android --- lib/utils/firebase_controller.dart | 24 +++++++++++++++++++++--- pubspec.yaml | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/utils/firebase_controller.dart b/lib/utils/firebase_controller.dart index 6b7ca8c..e01ef59 100644 --- a/lib/utils/firebase_controller.dart +++ b/lib/utils/firebase_controller.dart @@ -23,7 +23,7 @@ abstract class FirebaseController { static const String CHANNEL_ID = 'fluffychat_push'; static const String CHANNEL_NAME = 'FluffyChat push channel'; static const String CHANNEL_DESCRIPTION = 'Push notifications for FluffyChat'; - static const String APP_ID = 'chat.fluffy.fluffychat'; + static const String APP_ID = 'chat.fluffy.fluffychat.experimental'; static const String GATEWAY_URL = 'https://janian.de:7023/'; static const String PUSHER_FORMAT = 'event_id_only'; @@ -149,21 +149,29 @@ abstract class FirebaseController { return null; } if (context != null && Matrix.of(context).activeRoomId == roomId) { + debugPrint('[Push] New clearing push'); return null; } + debugPrint('[Push] New message received'); final i18n = context == null ? L10n(Platform.localeName) : L10n.of(context); // Get the client Client client; - if (context != null) { + var tempClient = false; + try { client = Matrix.of(context).client; - } else { + } catch (_) { + client = null; + } + if (client == null) { + tempClient = true; final platform = kIsWeb ? 'Web' : Platform.operatingSystem; final clientName = 'FluffyChat $platform'; client = Client(clientName); client.database = await getDatabase(client); client.connect(); + debugPrint('[Push] Use a temp client'); await client.onLoginStateChanged.stream .firstWhere((l) => l == LoginState.logged) .timeout( @@ -174,10 +182,12 @@ abstract class FirebaseController { // Get the room var room = client.getRoomById(roomId); if (room == null) { + debugPrint('[Push] Wait for the room'); await client.onRoomUpdate.stream .where((u) => u.id == roomId) .first .timeout(Duration(seconds: 5)); + debugPrint('[Push] Room found'); room = client.getRoomById(roomId); if (room == null) return null; } @@ -185,10 +195,12 @@ abstract class FirebaseController { // Get the event var event = await client.database.getEventById(client.id, eventId, room); if (event == null) { + debugPrint('[Push] Wait for the event'); final eventUpdate = await client.onEvent.stream .where((u) => u.content['event_id'] == eventId) .first .timeout(Duration(seconds: 5)); + debugPrint('[Push] Event found'); event = Event.fromJson(eventUpdate.content, room); if (room == null) return null; } @@ -249,6 +261,12 @@ abstract class FirebaseController { await _flutterLocalNotificationsPlugin.show( 0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics, payload: roomId); + + if (tempClient) { + await client.dispose(); + client = null; + debugPrint('[Push] Temp client disposed'); + } } catch (exception) { debugPrint('[Push] Error while processing notification: ' + exception.toString()); diff --git a/pubspec.yaml b/pubspec.yaml index b25e96a..8c4db9b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,7 +35,7 @@ dependencies: url_launcher: ^5.4.1 url_launcher_web: ^0.1.0 cached_network_image: ^2.3.1 - firebase_messaging: ^6.0.13 + firebase_messaging: ^7.0.0 flutter_local_notifications: ^1.4.3 matrix_link_text: ^0.1.5 path_provider: ^1.5.1