Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(neon_framework): Handle delete-multiple background push notifications #2603

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell/nextcloud.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mountpoint
navigations
nextcloud
nextcloud's
nids
organisation
partlycloudy
pollinterval
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
Expand Down Expand Up @@ -35,9 +36,12 @@ abstract class DecryptedSubject implements Built<DecryptedSubject, DecryptedSubj
// ignore: public_member_api_docs
static Serializer<DecryptedSubject> get serializer => _$decryptedSubjectSerializer;

/// ID if the notification.
/// ID of the notification.
int? get nid;

/// IDs of multiple notifications.
BuiltList<int>? get nids;

/// App that sent the notification.
String? get app;

Expand All @@ -53,12 +57,17 @@ abstract class DecryptedSubject implements Built<DecryptedSubject, DecryptedSubj
/// Delete the notification.
bool? get delete;

/// Delete multiple notifications.
@BuiltValueField(wireName: 'delete-multiple')
bool? get deleteMultiple;

/// Delete all notifications.
@BuiltValueField(wireName: 'delete-all')
bool? get deleteAll;
}

final Serializers _serializers = (Serializers().toBuilder()
..add(DecryptedSubject.serializer)
..addBuilderFactory(const FullType(BuiltList, [FullType(int)]), ListBuilder<int>.new)
..addPlugin(StandardJsonPlugin()))
.build();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.