Skip to content

Commit

Permalink
fix(neon_framework): Handle delete-multiple background push notificat…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Oct 28, 2024
1 parent c4be1b2 commit 86c401f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/neon_framework/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ class PushUtils {
final pushNotifications = await parseEncryptedPushNotifications(storage, encryptedPushNotifications, accountID);
for (final pushNotification in pushNotifications) {
if (pushNotification.subject.delete ?? false) {
await localNotificationsPlugin.cancel(_getNotificationID(accountID, pushNotification));
await localNotificationsPlugin.cancel(_getNotificationID(accountID, pushNotification.subject.nid!));
} else if (pushNotification.subject.deleteMultiple ?? false) {
await Future.wait([
for (final nid in pushNotification.subject.nids!)
localNotificationsPlugin.cancel(_getNotificationID(accountID, nid)),
]);
} else if (pushNotification.subject.deleteAll ?? false) {
await localNotificationsPlugin.cancelAll();
} else if (pushNotification.type == 'background') {
Expand Down Expand Up @@ -164,7 +169,7 @@ class PushUtils {
final when = notification != null ? tz.TZDateTime.parse(tz.UTC, notification.datetime) : null;

await localNotificationsPlugin.show(
_getNotificationID(accountID, pushNotification),
_getNotificationID(accountID, pushNotification.subject.nid!),
message != null && appName != null ? '$appName: $title' : title,
message,
NotificationDetails(
Expand Down Expand Up @@ -289,8 +294,8 @@ class PushUtils {
return ByteArrayAndroidBitmap(img.encodeBmp(img.decodePng(bytes!.buffer.asUint8List())!));
}

static int _getNotificationID(String accountID, PushNotification notification) {
return sha256.convert(utf8.encode('$accountID${notification.subject.nid}')).bytes.reduce((a, b) => a + b);
static int _getNotificationID(String accountID, int nid) {
return sha256.convert(utf8.encode('$accountID$nid')).bytes.reduce((a, b) => a + b);
}

static int _getGroupSummaryID(String accountID, String app) {
Expand Down
16 changes: 16 additions & 0 deletions packages/neon_framework/test/push_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ void main() {
verify(() => localNotificationsPlatform.cancel(3811)).called(1);
});

test('Delete multiple', () async {
final payload = {
'priority': '',
'type': '',
'subject': {
'nids': [1, 2],
'delete-multiple': true,
},
};

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

verify(() => localNotificationsPlatform.cancel(3811)).called(1);
verify(() => localNotificationsPlatform.cancel(4269)).called(1);
});

test('Delete all', () async {
final payload = {
'priority': '',
Expand Down

0 comments on commit 86c401f

Please sign in to comment.