Skip to content

Commit

Permalink
Fix empty notifications due to notification refactor (#1324)
Browse files Browse the repository at this point in the history
* Fix empty notifications

* Fix duplicate channels

* Fix id match
  • Loading branch information
micahmo authored Apr 24, 2024
1 parent 436b12f commit 09e4848
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
16 changes: 13 additions & 3 deletions lib/comment/utils/comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,24 @@ bool updateModifiedComment(List<CommentViewTree> commentTrees, CommentView comme
}

String cleanCommentContent(Comment comment) {
final AppLocalizations l10n = AppLocalizations.of(GlobalContext.context)!;
String deletedByModerator = "deleted by moderator";
String deletedByCreator = "deleted by creator";

try {
// Try to load these strings from localizations
final AppLocalizations l10n = AppLocalizations.of(GlobalContext.context)!;
deletedByModerator = l10n.deletedByModerator;
deletedByCreator = l10n.deletedByCreator;
} catch (e) {
// Ignore the error and move on with the default strings
}

if (comment.removed) {
return '_${l10n.deletedByModerator}_';
return '_${deletedByModerator}_';
}

if (comment.deleted) {
return '_${l10n.deletedByCreator}_';
return '_${deletedByCreator}_';
}

return comment.content;
Expand Down
13 changes: 10 additions & 3 deletions lib/notification/shared/android_notification.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Package imports
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:shared_preferences/shared_preferences.dart';

// Project imports
import 'package:thunder/account/models/account.dart';
import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/core/enums/local_settings.dart';
import 'package:thunder/core/singletons/preferences.dart';

const String _inboxMessagesChannelId = 'inbox_messages';
const String _inboxMessagesChannelName = 'Inbox Messages';
const String repliesGroupKey = 'replies';

Expand All @@ -13,17 +18,19 @@ const String repliesGroupKey = 'replies';
/// to help display a group of notifications on Android.
void showNotificationGroups({List<Account> accounts = const []}) async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final SharedPreferences prefs = (await UserPreferences.instance).sharedPreferences;
final FullNameSeparator userSeparator = FullNameSeparator.values.byName(prefs.getString(LocalSettings.userFormat.name) ?? FullNameSeparator.at.name);

for (Account account in accounts) {
// Create a summary notification for the group.
final InboxStyleInformation inboxStyleInformationSummary = InboxStyleInformation(
[],
contentTitle: '',
summaryText: '${account.username}@${account.instance}',
summaryText: generateUserFullName(null, account.username, account.instance, userSeparator: userSeparator),
);

final AndroidNotificationDetails androidNotificationDetailsSummary = AndroidNotificationDetails(
account.id,
_inboxMessagesChannelId,
_inboxMessagesChannelName,
styleInformation: inboxStyleInformationSummary,
groupKey: account.id,
Expand Down Expand Up @@ -57,7 +64,7 @@ void showAndroidNotification({

// Configure Android-specific settings
final AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
account?.id ?? 'default',
_inboxMessagesChannelId,
_inboxMessagesChannelName,
styleInformation: bigTextStyleInformation,
groupKey: account?.id ?? 'default',
Expand Down
2 changes: 1 addition & 1 deletion lib/notification/utils/local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Future<void> pollRepliesAndShowNotifications() async {
bigTextStyleInformation: bigTextStyleInformation,
title: generateUserFullName(null, commentReplyView.creator.name, fetchInstanceNameFromUrl(commentReplyView.creator.actorId), userSeparator: userSeparator),
content: plaintextComment,
payload: '$repliesGroupKey-${commentReplyView.comment.id}',
payload: '$repliesGroupKey-${commentReplyView.commentReply.id}',
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/notification/utils/unified_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
bigTextStyleInformation: bigTextStyleInformation,
title: generateUserFullName(null, commentReplyView.creator.name, fetchInstanceNameFromUrl(commentReplyView.creator.actorId), userSeparator: userSeparator),
content: plaintextComment,
payload: '$repliesGroupKey-${commentReplyView.comment.id}',
payload: '$repliesGroupKey-${commentReplyView.commentReply.id}',
);
}

Expand Down Expand Up @@ -124,7 +124,7 @@ void initUnifiedPushNotifications({required StreamController<NotificationRespons
bigTextStyleInformation: bigTextStyleInformation,
title: generateUserFullName(null, personMentionView.creator.name, fetchInstanceNameFromUrl(personMentionView.creator.actorId), userSeparator: userSeparator),
content: plaintextComment,
payload: '$repliesGroupKey-${personMentionView.comment.id}',
payload: '$repliesGroupKey-${personMentionView.personMention.id}',
);
}

Expand Down

0 comments on commit 09e4848

Please sign in to comment.