diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart index 579b9745..4fb75d0b 100644 --- a/lib/utils/background_push.dart +++ b/lib/utils/background_push.dart @@ -131,84 +131,34 @@ class BackgroundPush { await _fcmSharedIsolate?.requestPermission(); } final clientName = PlatformInfos.clientName; - oldTokens ??= {}; - final pushers = await client.getPushers().catchError((e) { - Logs().w('[Push] Unable to request pushers', e); - return []; - }); - var setNewPusher = false; // Just the plain app id, we add the .data_message suffix later var appId = AppConfig.pushNotificationsAppId; // we need the deviceAppId to remove potential legacy UP pusher var deviceAppId = '$appId.${client.deviceID}'; // appId may only be up to 64 chars as per spec - if (deviceAppId.length > 64) { - deviceAppId = deviceAppId.substring(0, 64); + final maxLength = 64 - '.data_message'.length; + if (deviceAppId.length > maxLength) { + deviceAppId = deviceAppId.substring(0, maxLength); } if (!useDeviceSpecificAppId && PlatformInfos.isAndroid) { appId += '.data_message'; } final thisAppId = useDeviceSpecificAppId ? deviceAppId : appId; - if (gatewayUrl != null && token != null && clientName != null) { - final currentPushers = pushers.where((pusher) => pusher.pushkey == token); - if (currentPushers.length == 1 && - currentPushers.first.kind == 'http' && - currentPushers.first.appId == thisAppId && - currentPushers.first.appDisplayName == clientName && - currentPushers.first.deviceDisplayName == client.deviceName && - currentPushers.first.lang == 'en' && - currentPushers.first.data.url.toString() == gatewayUrl && - currentPushers.first.data.format == - AppConfig.pushNotificationsPusherFormat) { - Logs().i('[Push] Pusher already set'); - } else { - Logs().i('Need to set new pusher'); - oldTokens.add(token); - if (client.isLogged()) { - setNewPusher = true; - } - } - } else { - Logs().w('[Push] Missing required push credentials'); - } - for (final pusher in pushers) { - if ((token != null && - pusher.pushkey != token && - deviceAppId == pusher.appId) || - oldTokens.contains(pusher.pushkey)) { - pusher.kind = null; - try { - await client.postPusher( - pusher, - append: true, - ); - Logs().i('[Push] Removed legacy pusher for this device'); - } catch (err) { - Logs().w('[Push] Failed to remove old pusher', err); - } - } - } - if (setNewPusher) { - try { - await client.postPusher( - Pusher( - pushkey: token, - appId: thisAppId, - appDisplayName: clientName, - deviceDisplayName: client.deviceName, - lang: 'en', - data: PusherData( - url: Uri.parse(gatewayUrl), - format: AppConfig.pushNotificationsPusherFormat, - ), - kind: 'http', - ), - append: false, - ); - } catch (e, s) { - Logs().e('[Push] Unable to set pushers', e, s); - } - } + await client.setupPusher( + oldTokens: oldTokens, + pusher: Pusher( + pushkey: token, + appId: thisAppId, + appDisplayName: clientName, + deviceDisplayName: client.deviceName, + lang: 'en', + data: PusherData( + url: Uri.parse(gatewayUrl), + format: AppConfig.pushNotificationsPusherFormat, + ), + kind: 'http', + ), + ); } bool _wentToRoomOnStartup = false; @@ -325,7 +275,7 @@ class BackgroundPush { Logs().v('[Push] Foreground message received'); Map data; try { - data = Map.from(message['data'] ?? message); + data = await client.processPushPayload(Map.from(message)); await _onMessage(data); } catch (e, s) { Logs().e('[Push] Error while processing notification', e, s); @@ -392,7 +342,7 @@ class BackgroundPush { Future _onUpMessage(String message) async { Map data; try { - data = Map.from(json.decode(message)['notification']); + data = await client.processPushPayload(json.decode(message)); await _onMessage(data); } catch (e, s) { Logs().e('[Push] Error while processing notification', e, s); diff --git a/pubspec.lock b/pubspec.lock index c3a9fcd1..8d946aca 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -815,17 +815,17 @@ packages: matrix: dependency: "direct main" description: - name: matrix - url: "https://pub.dartlang.org" - source: hosted - version: "0.7.0" + path: "/home/sorunome/repos/famedly/famedlysdk" + relative: false + source: path + version: "0.6.2" matrix_api_lite: dependency: transitive description: - name: matrix_api_lite - url: "https://pub.dartlang.org" - source: hosted - version: "0.5.3" + path: "/home/sorunome/repos/famedly/frontend/libraries/matrix_api_lite" + relative: false + source: path + version: "0.4.4" matrix_link_text: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 2bfd07f9..bd13fd80 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -48,7 +48,9 @@ dependencies: intl: any localstorage: ^4.0.0+1 lottie: ^1.2.1 - matrix: ^0.7.0 + #matrix: ^0.7.0 + matrix: + path: /home/sorunome/repos/famedly/famedlysdk matrix_link_text: ^1.0.2 open_noti_settings: ^0.3.0 package_info_plus: ^1.2.1 @@ -113,4 +115,4 @@ dependency_overrides: hosted: name: geolocator_android url: https://hanntech-gmbh.gitlab.io/free2pass/flutter-geolocator-floss - provider: 5.0.0 \ No newline at end of file + provider: 5.0.0