feat: Implement rich push notifications on android

This commit is contained in:
Christian Pauly 2020-09-17 08:34:36 +02:00
parent a7d5a97219
commit f4e4b90eab
2 changed files with 22 additions and 4 deletions

View File

@ -23,7 +23,7 @@ abstract class FirebaseController {
static const String CHANNEL_ID = 'fluffychat_push'; static const String CHANNEL_ID = 'fluffychat_push';
static const String CHANNEL_NAME = 'FluffyChat push channel'; static const String CHANNEL_NAME = 'FluffyChat push channel';
static const String CHANNEL_DESCRIPTION = 'Push notifications for FluffyChat'; 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 GATEWAY_URL = 'https://janian.de:7023/';
static const String PUSHER_FORMAT = 'event_id_only'; static const String PUSHER_FORMAT = 'event_id_only';
@ -149,21 +149,29 @@ abstract class FirebaseController {
return null; return null;
} }
if (context != null && Matrix.of(context).activeRoomId == roomId) { if (context != null && Matrix.of(context).activeRoomId == roomId) {
debugPrint('[Push] New clearing push');
return null; return null;
} }
debugPrint('[Push] New message received');
final i18n = final i18n =
context == null ? L10n(Platform.localeName) : L10n.of(context); context == null ? L10n(Platform.localeName) : L10n.of(context);
// Get the client // Get the client
Client client; Client client;
if (context != null) { var tempClient = false;
try {
client = Matrix.of(context).client; client = Matrix.of(context).client;
} else { } catch (_) {
client = null;
}
if (client == null) {
tempClient = true;
final platform = kIsWeb ? 'Web' : Platform.operatingSystem; final platform = kIsWeb ? 'Web' : Platform.operatingSystem;
final clientName = 'FluffyChat $platform'; final clientName = 'FluffyChat $platform';
client = Client(clientName); client = Client(clientName);
client.database = await getDatabase(client); client.database = await getDatabase(client);
client.connect(); client.connect();
debugPrint('[Push] Use a temp client');
await client.onLoginStateChanged.stream await client.onLoginStateChanged.stream
.firstWhere((l) => l == LoginState.logged) .firstWhere((l) => l == LoginState.logged)
.timeout( .timeout(
@ -174,10 +182,12 @@ abstract class FirebaseController {
// Get the room // Get the room
var room = client.getRoomById(roomId); var room = client.getRoomById(roomId);
if (room == null) { if (room == null) {
debugPrint('[Push] Wait for the room');
await client.onRoomUpdate.stream await client.onRoomUpdate.stream
.where((u) => u.id == roomId) .where((u) => u.id == roomId)
.first .first
.timeout(Duration(seconds: 5)); .timeout(Duration(seconds: 5));
debugPrint('[Push] Room found');
room = client.getRoomById(roomId); room = client.getRoomById(roomId);
if (room == null) return null; if (room == null) return null;
} }
@ -185,10 +195,12 @@ abstract class FirebaseController {
// Get the event // Get the event
var event = await client.database.getEventById(client.id, eventId, room); var event = await client.database.getEventById(client.id, eventId, room);
if (event == null) { if (event == null) {
debugPrint('[Push] Wait for the event');
final eventUpdate = await client.onEvent.stream final eventUpdate = await client.onEvent.stream
.where((u) => u.content['event_id'] == eventId) .where((u) => u.content['event_id'] == eventId)
.first .first
.timeout(Duration(seconds: 5)); .timeout(Duration(seconds: 5));
debugPrint('[Push] Event found');
event = Event.fromJson(eventUpdate.content, room); event = Event.fromJson(eventUpdate.content, room);
if (room == null) return null; if (room == null) return null;
} }
@ -249,6 +261,12 @@ abstract class FirebaseController {
await _flutterLocalNotificationsPlugin.show( await _flutterLocalNotificationsPlugin.show(
0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics, 0, room.getLocalizedDisplayname(i18n), body, platformChannelSpecifics,
payload: roomId); payload: roomId);
if (tempClient) {
await client.dispose();
client = null;
debugPrint('[Push] Temp client disposed');
}
} catch (exception) { } catch (exception) {
debugPrint('[Push] Error while processing notification: ' + debugPrint('[Push] Error while processing notification: ' +
exception.toString()); exception.toString());

View File

@ -35,7 +35,7 @@ dependencies:
url_launcher: ^5.4.1 url_launcher: ^5.4.1
url_launcher_web: ^0.1.0 url_launcher_web: ^0.1.0
cached_network_image: ^2.3.1 cached_network_image: ^2.3.1
firebase_messaging: ^6.0.13 firebase_messaging: ^7.0.0
flutter_local_notifications: ^1.4.3 flutter_local_notifications: ^1.4.3
matrix_link_text: ^0.1.5 matrix_link_text: ^0.1.5
path_provider: ^1.5.1 path_provider: ^1.5.1