Skip to content

Commit

Permalink
fix(neon_framework): Automatically cancel summary notification if needed
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Oct 28, 2024
1 parent 1448989 commit 744bdda
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/neon_framework/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ class PushUtils {
} else {
_log.fine('Got unknown background notification ${json.encode(pushNotification.toJson())}');
}

if (defaultTargetPlatform == TargetPlatform.android) {
final groups = <String, List<int>>{};

// Count how many notifications per group still exist.
final activeNotifications = await localNotificationsPlugin.getActiveNotifications();
for (final activeNotification in activeNotifications) {
final id = activeNotification.id;
final groupKey = activeNotification.groupKey;
if (id != null && groupKey != null) {
groups[groupKey] ??= [];
groups[groupKey]!.add(id);
}
}

for (final entry in groups.entries) {
// If there is only one notification left it has to be the summary,
// because the child notifications are automatically canceled when the user cancels the summary notification.
if (entry.value.length == 1) {
await localNotificationsPlugin.cancel(getGroupSummaryID(accountID, entry.key));
}
}
}
} else {
packageInfo ??= await PackageInfo.fromPlatform();
accountStorage ??= AccountStorage(
Expand Down
39 changes: 39 additions & 0 deletions packages/neon_framework/test/push_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void main() {
).thenAnswer((_) async {});
when(() => localNotificationsPlatform.cancel(any(), tag: any(named: 'tag'))).thenAnswer((_) async {});
when(() => localNotificationsPlatform.cancelAll()).thenAnswer((_) async {});
when(() => localNotificationsPlatform.getActiveNotifications()).thenAnswer((_) async => []);

keypair = RSAKeypair.fromRandom();

Expand Down Expand Up @@ -205,6 +206,7 @@ void main() {
await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);

verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 1))).called(1);
verify(() => localNotificationsPlatform.getActiveNotifications()).called(1);
});

test('Delete multiple', () async {
Expand All @@ -221,6 +223,7 @@ void main() {

verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 1))).called(1);
verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 2))).called(1);
verify(() => localNotificationsPlatform.getActiveNotifications()).called(1);
});

test('Delete all', () async {
Expand All @@ -235,6 +238,7 @@ void main() {
await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);

verify(() => localNotificationsPlatform.cancelAll()).called(1);
verify(() => localNotificationsPlatform.getActiveNotifications()).called(1);
});

test('Background', () async {
Expand All @@ -245,6 +249,41 @@ void main() {
};

await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);
verify(() => localNotificationsPlatform.getActiveNotifications()).called(1);
});

test('Cancel summary notification', () async {
when(() => localNotificationsPlatform.getActiveNotifications()).thenAnswer(
(_) async => [
const ActiveNotification(
id: 123,
groupKey: 'app1',
),
const ActiveNotification(
id: 456,
groupKey: 'app2',
),
const ActiveNotification(
id: 789,
groupKey: 'app2',
),
],
);

final payload = {
'priority': '',
'type': 'background',
'subject': {
'nid': 1,
'delete': true,
},
};

await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);

verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 1))).called(1);
verify(() => localNotificationsPlatform.getActiveNotifications()).called(1);
verify(() => localNotificationsPlatform.cancel(PushUtils.getGroupSummaryID(account.id, 'app1'))).called(1);
});

group('Message', () {
Expand Down

0 comments on commit 744bdda

Please sign in to comment.