@@ -3,6 +3,7 @@ import 'dart:typed_data';
33import 'dart:ui' ;
44import 'dart:convert' ;
55import 'dart:isolate' ;
6+ import 'package:flutter/services.dart' ;
67import 'package:firebase_messaging/firebase_messaging.dart' ;
78import 'package:flutter_local_notifications/flutter_local_notifications.dart' ;
89import 'package:http/http.dart' as http;
@@ -275,7 +276,9 @@ Future<void> _onReceiveMessageFromPort(RemoteMessage firebaseMessage) async {
275276 );
276277}
277278
278- void _onSelectNotification (String ? payload) {
279+ void _onSelectNotification (NotificationResponse details) {
280+ final payload = details.payload;
281+
279282 print ('📘 _onSelectNotification: $payload ' );
280283
281284 if (payload != null ) {
@@ -305,23 +308,43 @@ Future<void> registerAndroidPushNotificationHandlers(AndroidChannel androidChann
305308 InitializationSettings (
306309 android: AndroidInitializationSettings ('@mipmap/ic_launcher' ),
307310 ),
308- onSelectNotification : _onSelectNotification,
311+ onDidReceiveNotificationResponse : _onSelectNotification,
309312 );
310313
311314 _androidChannel = androidChannel;
312315
313- // TODO: Handle already existing notifications
314- /* Already existing notifications cannot be handled at the moment because
315- * the ActiveNotification class doesn't have enough information
316316 try {
317317 final activeNotifications = await _flutterLocalNotificationsPlugin
318318 .resolvePlatformSpecificImplementation <AndroidFlutterLocalNotificationsPlugin >()
319319 ? .getActiveNotifications ();
320+
321+ if (activeNotifications != null ) {
322+ for (final displayedNotification in activeNotifications) {
323+ if (displayedNotification.payload != null ) {
324+ final Map <String , dynamic > talkjsData = json.decode (displayedNotification.payload! );
325+
326+ if ((talkjsData['conversation' ] != null ) && (talkjsData['conversation' ]['id' ] != null )) {
327+ print ('📘 Existing notification: ${displayedNotification .payload }' );
328+ final String notificationId = talkjsData['conversation' ]['id' ];
329+
330+ if (! _showIdFromNotificationId.containsKey (notificationId)) {
331+ _showIdFromNotificationId[notificationId] = _nextId;
332+ _nextId += 1 ;
333+ }
334+
335+ if (_activeNotifications[notificationId] == null ) {
336+ _activeNotifications[notificationId] = [displayedNotification.payload! ];
337+ } else {
338+ _activeNotifications[notificationId]! .add (displayedNotification.payload! );
339+ }
340+ }
341+ }
342+ }
343+ }
320344 } on PlatformException {
321345 // PlatformException is raised on Android < 6.0
322346 // Simply ignoring this part
323347 }
324- */
325348
326349 IsolateNameServer .registerPortWithName (_receivePort.sendPort, 'talkjsFCMPort' );
327350 _receivePort.listen ((message) async => await _onReceiveMessageFromPort (message));
0 commit comments