Skip to content

Commit 909fb3d

Browse files
author
Franco Bugnano
committed
Track existing push notifications at startup
1 parent f4ffe68 commit 909fb3d

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

examples/push_notifications/pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,21 @@ packages:
159159
name: flutter_local_notifications
160160
url: "https://pub.dartlang.org"
161161
source: hosted
162-
version: "9.5.2"
162+
version: "10.0.0-dev.14"
163163
flutter_local_notifications_linux:
164164
dependency: transitive
165165
description:
166166
name: flutter_local_notifications_linux
167167
url: "https://pub.dartlang.org"
168168
source: hosted
169-
version: "0.4.2"
169+
version: "0.5.0-dev.4"
170170
flutter_local_notifications_platform_interface:
171171
dependency: transitive
172172
description:
173173
name: flutter_local_notifications_platform_interface
174174
url: "https://pub.dartlang.org"
175175
source: hosted
176-
version: "5.0.0"
176+
version: "6.0.0-dev.4"
177177
flutter_test:
178178
dependency: "direct dev"
179179
description: flutter

lib/src/notification.dart

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:typed_data';
33
import 'dart:ui';
44
import 'dart:convert';
55
import 'dart:isolate';
6+
import 'package:flutter/services.dart';
67
import 'package:firebase_messaging/firebase_messaging.dart';
78
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
89
import '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));

pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,21 @@ packages:
145145
name: flutter_local_notifications
146146
url: "https://pub.dartlang.org"
147147
source: hosted
148-
version: "9.5.2"
148+
version: "10.0.0-dev.14"
149149
flutter_local_notifications_linux:
150150
dependency: transitive
151151
description:
152152
name: flutter_local_notifications_linux
153153
url: "https://pub.dartlang.org"
154154
source: hosted
155-
version: "0.4.2"
155+
version: "0.5.0-dev.4"
156156
flutter_local_notifications_platform_interface:
157157
dependency: transitive
158158
description:
159159
name: flutter_local_notifications_platform_interface
160160
url: "https://pub.dartlang.org"
161161
source: hosted
162-
version: "5.0.0"
162+
version: "6.0.0-dev.4"
163163
flutter_test:
164164
dependency: "direct dev"
165165
description: flutter

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies:
1515
crypto: ^3.0.1
1616
firebase_core: ^1.16.0
1717
firebase_messaging: ^11.3.0
18-
flutter_local_notifications: ^9.5.2
18+
flutter_local_notifications: ^10.0.0-dev.14
1919
http: ^0.13.4
2020
flutter_apns_only: ^1.5.2
2121

0 commit comments

Comments
 (0)