1
1
import 'dart:async' ;
2
2
import 'dart:io' ;
3
3
4
- import 'package:http/http.dart' as http;
5
4
import 'package:collection/collection.dart' ;
6
5
import 'package:flutter/foundation.dart' ;
7
6
import 'package:flutter/widgets.dart' hide Notification;
7
+ import 'package:http/http.dart' as http;
8
8
9
9
import '../api/model/model.dart' ;
10
10
import '../api/notifications.dart' ;
@@ -217,10 +217,12 @@ class NotificationChannelManager {
217
217
/// Service for managing the notifications shown to the user.
218
218
class NotificationDisplayManager {
219
219
static Future <void > init () async {
220
+ assert (defaultTargetPlatform == TargetPlatform .android, 'NotificationDisplayManager only supports Android' );
220
221
await NotificationChannelManager .ensureChannel ();
221
222
}
222
223
223
224
static void onFcmMessage (FcmMessage data, Map <String , dynamic > dataJson) {
225
+ assert (defaultTargetPlatform == TargetPlatform .android, 'NotificationDisplayManager only supports Android' );
224
226
switch (data) {
225
227
case MessageFcmMessage (): _onMessageFcmMessage (data, dataJson);
226
228
case RemoveFcmMessage (): _onRemoveFcmMessage (data);
@@ -231,9 +233,16 @@ class NotificationDisplayManager {
231
233
static Future <void > _onMessageFcmMessage (MessageFcmMessage data, Map <String , dynamic > dataJson) async {
232
234
assert (debugLog ('notif message content: ${data .content }' ));
233
235
final zulipLocalizations = GlobalLocalizations .zulipLocalizations;
234
- final groupKey = _groupKey (data);
236
+ final groupKey = _groupKey (data.realmUrl, data.userId );
235
237
final conversationKey = _conversationKey (data, groupKey);
236
238
239
+ final globalStore = await ZulipBinding .instance.getGlobalStore ();
240
+ final account = globalStore.accounts.firstWhereOrNull ((account) =>
241
+ account.realmUrl.origin == data.realmUrl.origin && account.userId == data.userId);
242
+ if (account == null ) {
243
+ return ;
244
+ }
245
+
237
246
final oldMessagingStyle = await _androidHost
238
247
.getActiveNotificationMessagingStyleByTag (conversationKey);
239
248
@@ -365,7 +374,7 @@ class NotificationDisplayManager {
365
374
// There may be a lot of messages mentioned here, across a lot of
366
375
// conversations. But they'll all be for one account, so they'll
367
376
// fall under one notification group.
368
- final groupKey = _groupKey (data);
377
+ final groupKey = _groupKey (data.realmUrl, data.userId );
369
378
370
379
// Find any conversations we can cancel the notification for.
371
380
// The API doesn't lend itself to removing individual messages as
@@ -445,10 +454,10 @@ class NotificationDisplayManager {
445
454
return '$groupKey |$conversation ' ;
446
455
}
447
456
448
- static String _groupKey (FcmMessageWithIdentity data ) {
457
+ static String _groupKey (Uri realmUrl, int userId ) {
449
458
// The realm URL can't contain a `|`, because `|` is not a URL code point:
450
459
// https://url.spec.whatwg.org/#url-code-points
451
- return "${ data . realmUrl }|${ data . userId } " ;
460
+ return "$realmUrl |$ userId " ;
452
461
}
453
462
454
463
static String _personKey (Uri realmUrl, int userId) => "$realmUrl |$userId " ;
@@ -464,6 +473,8 @@ class NotificationDisplayManager {
464
473
required BuildContext context,
465
474
required Uri url,
466
475
}) {
476
+ assert (defaultTargetPlatform == TargetPlatform .android);
477
+
467
478
final globalStore = GlobalStoreWidget .of (context);
468
479
469
480
assert (debugLog ('got notif: url: $url ' ));
@@ -492,6 +503,7 @@ class NotificationDisplayManager {
492
503
/// generated with [NotificationOpenPayload.buildUrl] while creating
493
504
/// the notification.
494
505
static Future <void > navigateForNotification (Uri url) async {
506
+ assert (defaultTargetPlatform == TargetPlatform .android);
495
507
assert (debugLog ('opened notif: url: $url ' ));
496
508
497
509
NavigatorState navigator = await ZulipApp .navigator;
@@ -518,6 +530,18 @@ class NotificationDisplayManager {
518
530
}
519
531
return null ;
520
532
}
533
+
534
+ static Future <void > removeNotificationsForAccount (Uri realmUri, int userId) async {
535
+ if (defaultTargetPlatform != TargetPlatform .android) return ;
536
+
537
+ final groupKey = _groupKey (realmUri, userId);
538
+ final activeNotifications = await _androidHost.getActiveNotifications (desiredExtras: [kExtraLastZulipMessageId]);
539
+ for (final statusBarNotification in activeNotifications) {
540
+ if (statusBarNotification.notification.group == groupKey) {
541
+ await _androidHost.cancel (tag: statusBarNotification.tag, id: statusBarNotification.id);
542
+ }
543
+ }
544
+ }
521
545
}
522
546
523
547
/// The information contained in 'zulip://notification/…' internal
0 commit comments