Skip to content

Commit

Permalink
fix(neon_framework): Group push notifications on Android
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Oct 24, 2024
1 parent afbf242 commit 781415e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/neon_framework/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui';

import 'package:account_repository/account_repository.dart';
import 'package:built_collection/built_collection.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_svg/flutter_svg.dart' show SvgBytesLoader, vg;
Expand Down Expand Up @@ -185,6 +185,29 @@ class PushUtils {
),
payload: json.encode(pushNotification.toJson()),
);

// Other platforms don't support grouping, so don't send another notification there.
if (defaultTargetPlatform == TargetPlatform.android) {
// Sine we only support Android SDK 24+, we can just generate an empty summary notification as it will not be shown anyway.
await localNotificationsPlugin.show(
_getGroupSummaryID(accountID, appID),
null,
null,
NotificationDetails(
android: AndroidNotificationDetails(
appID,
appName ?? appID,
groupKey: '${appID}_app',
setAsGroupSummary: true,
styleInformation: InboxStyleInformation(
[],
summaryText: appName ?? appID,
),
color: NcColors.primary,
),
),
);
}
}
}
}
Expand Down Expand Up @@ -269,4 +292,8 @@ class PushUtils {
static int _getNotificationID(String accountID, PushNotification notification) {
return sha256.convert(utf8.encode('$accountID${notification.subject.nid}')).bytes.reduce((a, b) => a + b);
}

static int _getGroupSummaryID(String accountID, String app) {
return sha256.convert(utf8.encode('$accountID$app')).bytes.reduce((a, b) => a + b);
}
}
72 changes: 72 additions & 0 deletions packages/neon_framework/test/push_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,30 @@ void main() {
payload: payload,
),
).called(1);
verify(
() => localNotificationsPlatform.show(
4082,
null,
null,
notificationDetails: any(
named: 'notificationDetails',
that: predicate<AndroidNotificationDetails>(
(d) =>
d.channelId == 'app' &&
d.channelName == 'app' &&
d.subText == null &&
d.groupKey == 'app_app' &&
d.icon == null &&
d.largeIcon == null &&
d.when == null &&
d.color == NcColors.primary &&
d.category == null &&
d.importance == Importance.defaultImportance &&
d.priority == Priority.defaultPriority,
),
),
),
).called(1);

onDidReceiveNotificationResponseCallback(
NotificationResponse(
Expand Down Expand Up @@ -437,6 +461,30 @@ void main() {
payload: payload,
),
).called(1);
verify(
() => localNotificationsPlatform.show(
4405,
null,
null,
notificationDetails: any(
named: 'notificationDetails',
that: predicate<AndroidNotificationDetails>(
(d) =>
d.channelId == 'files' &&
d.channelName == 'Files' &&
d.subText == null &&
d.groupKey == 'files_app' &&
d.icon == null &&
d.largeIcon == null &&
d.when == null &&
d.color == NcColors.primary &&
d.category == null &&
d.importance == Importance.defaultImportance &&
d.priority == Priority.defaultPriority,
),
),
),
).called(1);

verify(() => storage.requestCache).called(1);
});
Expand Down Expand Up @@ -485,6 +533,30 @@ void main() {
payload: payload,
),
).called(1);
verify(
() => localNotificationsPlatform.show(
4082,
null,
null,
notificationDetails: any(
named: 'notificationDetails',
that: predicate<AndroidNotificationDetails>(
(d) =>
d.channelId == 'app' &&
d.channelName == 'app' &&
d.subText == null &&
d.groupKey == 'app_app' &&
d.icon == null &&
d.largeIcon == null &&
d.when == null &&
d.color == NcColors.primary &&
d.category == null &&
d.importance == Importance.defaultImportance &&
d.priority == Priority.defaultPriority,
),
),
),
).called(1);
});
});
});
Expand Down

0 comments on commit 781415e

Please sign in to comment.