Browse Source

neon: Load whole notification for received push notification

pull/258/head
jld3103 2 years ago
parent
commit
3a546e2a63
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 2
      packages/neon/neon/lib/l10n/en.arb
  2. 1
      packages/neon/neon/lib/neon.dart
  3. 32
      packages/neon/neon/lib/src/blocs/accounts.dart
  4. 58
      packages/neon/neon/lib/src/utils/push_utils.dart

2
packages/neon/neon/lib/l10n/en.arb

@ -1,6 +1,6 @@
{
"@@locale": "en",
"appImplementationName": "{app, select, core{Server} files{Files} news{News} notes{Notes} notifications{Notifications} other{}}",
"appImplementationName": "{app, select, nextcloud{Nextcloud} core{Server} files{Files} news{News} notes{Notes} notifications{Notifications} other{}}",
"@appImplementationName": {
"placeholders": {
"app": {}

1
packages/neon/neon/lib/neon.dart

@ -5,7 +5,6 @@ import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:crypto/crypto.dart';
import 'package:file_picker/file_picker.dart';
import 'package:filesize/filesize.dart';

32
packages/neon/neon/lib/src/blocs/accounts.dart

@ -1,5 +1,7 @@
part of '../../neon.dart';
const _keyAccounts = 'accounts';
abstract class AccountsBlocEvents {
void addAccount(final Account account);
void removeAccount(final Account account);
@ -20,19 +22,12 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
this._globalOptions,
this._allAppImplementations,
) {
accounts.listen((final as) async {
_globalOptions.updateAccounts(as);
await _storage.setStringList(_keyAccounts, as.map((final a) => json.encode(a.toJson())).toList());
});
if (_storage.containsKey(_keyAccounts)) {
accounts.add(
_storage
.getStringList(_keyAccounts)!
.map((final a) => Account.fromJson(json.decode(a) as Map<String, dynamic>))
.toList(),
);
}
accounts
..add(loadAccounts(_storage))
..listen((final as) async {
_globalOptions.updateAccounts(as);
await _storage.setStringList(_keyAccounts, as.map((final a) => json.encode(a.toJson())).toList());
});
final as = accounts.value;
if (_globalOptions.rememberLastUsedAccount.value && _storage.containsKey(_keyLastUsedAccount)) {
@ -56,7 +51,6 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
final SharedPreferences _sharedPreferences;
final GlobalOptions _globalOptions;
final List<AppImplementation> _allAppImplementations;
final _keyAccounts = 'accounts';
final _keyLastUsedAccount = 'last-used-account';
final _accountsOptions = <String, AccountSpecificOptions>{};
@ -194,3 +188,13 @@ class AccountsBloc extends Bloc implements AccountsBlocEvents, AccountsBlocState
);
}
}
List<Account> loadAccounts(final AppStorage storage) {
if (storage.containsKey(_keyAccounts)) {
return storage
.getStringList(_keyAccounts)!
.map((final a) => Account.fromJson(json.decode(a) as Map<String, dynamic>))
.toList();
}
return [];
}

58
packages/neon/neon/lib/src/utils/push_utils.dart

@ -55,20 +55,20 @@ class PushUtils {
for (final message in Uri(query: utf8.decode(messages)).queryParameters.values) {
final data = json.decode(message) as Map<String, dynamic>;
final notification = PushNotification(
final pushNotification = PushNotification(
accountID: instance,
priority: data['priority']! as String,
type: data['type']! as String,
subject: decryptPushNotificationSubject(keypair.privateKey, data['subject']! as String),
);
if (notification.subject.delete ?? false) {
await localNotificationsPlugin.cancel(_getNotificationID(instance, notification));
} else if (notification.subject.deleteAll ?? false) {
if (pushNotification.subject.delete ?? false) {
await localNotificationsPlugin.cancel(_getNotificationID(instance, pushNotification));
} else if (pushNotification.subject.deleteAll ?? false) {
await localNotificationsPlugin.cancelAll();
Global.onPushNotificationReceived?.call(instance);
} else if (notification.type == 'background') {
debugPrint('Got unknown background notification ${json.encode(notification.toJson())}');
} else if (pushNotification.type == 'background') {
debugPrint('Got unknown background notification ${json.encode(pushNotification.toJson())}');
} else {
final localizations = await appLocalizationsFromSystem();
@ -76,38 +76,46 @@ class PushUtils {
final cache = Cache(platform);
await cache.init();
var appName = localizations.appImplementationName(notification.subject.app ?? '');
NextcloudNotificationsNotification? notification;
try {
final account = loadAccounts(AppStorage('accounts', sharedPreferences)).find(instance);
if (account != null) {
notification =
(await account.client.notifications.getNotification(id: pushNotification.subject.nid!)).ocs.data;
}
} catch (e, s) {
debugPrint(e.toString());
debugPrint(s.toString());
}
final appID = notification?.app ?? pushNotification.subject.app ?? 'nextcloud';
var appName = localizations.appImplementationName(appID);
if (appName == '') {
debugPrint('Missing app name for ${notification.subject.app}');
appName = notification.subject.app ?? 'Nextcloud';
debugPrint('Missing app name for $appID');
appName = appID;
}
final title = notification?.subject ?? pushNotification.subject.subject;
final message = notification != null && notification.message != '' ? notification.message : null;
await localNotificationsPlugin.show(
_getNotificationID(instance, notification),
appName,
notification.subject.subject,
_getNotificationID(instance, pushNotification),
message != null ? '$appName: $title' : appName,
message ?? title,
NotificationDetails(
android: AndroidNotificationDetails(
notification.subject.app ?? 'nextcloud',
appID,
appName,
groupKey: notification.subject.app != null ? 'app_${notification.subject.app}' : 'nextcloud',
groupKey: 'app_$appID',
icon: '@mipmap/ic_launcher',
color: themePrimaryColor,
category: notification.type == 'voip' ? AndroidNotificationCategory.call : null,
category: pushNotification.type == 'voip' ? AndroidNotificationCategory.call : null,
importance: Importance.max,
priority: notification.priority == 'high'
? (notification.type == 'voip' ? Priority.max : Priority.high)
priority: pushNotification.priority == 'high'
? (pushNotification.type == 'voip' ? Priority.max : Priority.high)
: Priority.defaultPriority,
),
),
payload: json.encode(
PushNotification(
accountID: instance,
priority: notification.priority,
type: notification.type,
subject: notification.subject,
).toJson(),
),
payload: json.encode(pushNotification.toJson()),
);
}

Loading…
Cancel
Save