From 6aec3dd25ced7d942b3a0692d2eb3f4b43338d06 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 27 Apr 2022 08:54:54 +0200 Subject: [PATCH] refactor: Switch to UP for FCM --- android/app/build.gradle | 4 +- android/app/src/main/AndroidManifest.xml | 16 +-- .../fluffy/fluffychat/EmbeddedDistributor.kt | 14 +++ .../chat/fluffy/fluffychat/FcmPushService.kt | 36 ------ android/build.gradle | 1 - lib/utils/background_push.dart | 111 ++++-------------- scripts/build-ios.sh | 1 - scripts/code_analyze.sh | 2 - scripts/enable-android-google-services.patch | 91 -------------- scripts/prepare-android-release.sh | 1 - scripts/release-ios-testflight.sh | 1 - 11 files changed, 47 insertions(+), 231 deletions(-) create mode 100644 android/app/src/main/kotlin/chat/fluffy/fluffychat/EmbeddedDistributor.kt delete mode 100644 android/app/src/main/kotlin/chat/fluffy/fluffychat/FcmPushService.kt delete mode 100644 scripts/enable-android-google-services.patch diff --git a/android/app/build.gradle b/android/app/build.gradle index ad9ffb875..b74ce8952 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -82,11 +82,9 @@ flutter { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - //implementation 'com.google.firebase:firebase-messaging:19.0.1' // Workaround for https://github.com/microg/android_packages_apps_GmsCore/issues/313#issuecomment-617651698 + implementation('com.github.UnifiedPush:android-foss_embedded_fcm_distributor:1.0.0-beta1') testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'androidx.multidex:multidex:2.0.1' } - -//apply plugin: 'com.google.gms.google-services' diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index e219194db..572b82eb2 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -103,13 +103,6 @@ - - - - - - @@ -124,6 +117,15 @@ + + + + + + + + + 'Cannot get firebase token'; } @@ -52,7 +50,6 @@ class BackgroundPush { Client client; BuildContext? context; GlobalKey? router; - String? _fcmToken; void Function(String errorMsg, {Uri? link})? onFcmError; L10n? l10n; Store? _store; @@ -65,8 +62,6 @@ class BackgroundPush { final pendingTests = >{}; - final dynamic firebase = null; //FcmSharedIsolate(); - DateTime? lastReceivedPush; bool upAction = false; @@ -77,17 +72,6 @@ class BackgroundPush { onRoomSync ??= client.onSync.stream .where((s) => s.hasRoomUpdate) .listen((s) => _onClearingPush(getFromServer: false)); - firebase?.setListeners( - onMessage: (message) => pushHelper( - PushNotification.fromJson( - Map.from(message['data'] ?? message)), - client: client, - l10n: l10n, - activeRoomId: router?.currentState?.pathParameters['roomid'], - onSelectNotification: goToRoom, - ), - onNewToken: _newFcmToken, - ); if (Platform.isAndroid) { UnifiedPush.initialize( onNewEndpoint: _newUpEndpoint, @@ -123,20 +107,13 @@ class BackgroundPush { StreamSubscription? onLogin; StreamSubscription? onRoomSync; - void _newFcmToken(String token) { - _fcmToken = token; - setupPush(); - } - Future setupPusher({ String? gatewayUrl, String? token, Set? oldTokens, bool useDeviceSpecificAppId = false, }) async { - if (PlatformInfos.isIOS) { - await firebase?.requestPermission(); - } + print('Setup pusher $gatewayUrl $token'); final clientName = PlatformInfos.clientName; oldTokens ??= {}; final pushers = await (client.getPushers().catchError((e) { @@ -232,8 +209,6 @@ class BackgroundPush { if (!PlatformInfos.isIOS && (await UnifiedPush.getDistributors()).isNotEmpty) { await setupUp(); - } else { - await setupFirebase(); } // ignore: unawaited_futures @@ -251,46 +226,6 @@ class BackgroundPush { }); } - Future _noFcmWarning() async { - if (context == null) { - return; - } - if (await store.getItemBool(SettingKeys.showNoGoogle, true)) { - await loadLocale(); - if (PlatformInfos.isAndroid) { - onFcmError?.call( - l10n!.noGoogleServicesWarning, - link: Uri.parse( - AppConfig.enablePushTutorial, - ), - ); - } - onFcmError?.call(l10n!.oopsPushError); - - if (null == await store.getItem(SettingKeys.showNoGoogle)) { - await store.setItemBool(SettingKeys.showNoGoogle, false); - } - } - } - - Future setupFirebase() async { - Logs().v('Setup firebase'); - if (_fcmToken?.isEmpty ?? true) { - try { - _fcmToken = await firebase?.getToken(); - if (_fcmToken == null) throw ('PushToken is null'); - } catch (e, s) { - Logs().w('[Push] cannot get token', e, e is String ? null : s); - await _noFcmWarning(); - return; - } - } - await setupPusher( - gatewayUrl: AppConfig.pushNotificationsGatewayUrl, - token: _fcmToken, - ); - } - Future goToRoom(String? roomId) async { try { Logs().v('[Push] Attempting to go to room $roomId...'); @@ -316,6 +251,7 @@ class BackgroundPush { } Future _newUpEndpoint(String newEndpoint, String i) async { + print('New UP endpoint'); upAction = true; if (newEndpoint.isEmpty) { await _upUnregistered(i); @@ -324,36 +260,35 @@ class BackgroundPush { var endpoint = 'https://matrix.gateway.unifiedpush.org/_matrix/push/v1/notify'; try { - final url = Uri.parse(newEndpoint) - .replace( - path: '/_matrix/push/v1/notify', - query: '', - ) - .toString() - .split('?') - .first; - final res = - json.decode(utf8.decode((await http.get(Uri.parse(url))).bodyBytes)); - if (res['gateway'] == 'matrix' || - (res['unifiedpush'] is Map && - res['unifiedpush']['gateway'] == 'matrix')) { - endpoint = url; + const matrixGatewayPath = '/_matrix/push/v1/notify'; + if (Uri.parse(newEndpoint).path == matrixGatewayPath) { + endpoint = newEndpoint.split('?').first; + } else { + final url = Uri.parse(newEndpoint) + .replace( + path: matrixGatewayPath, + query: '', + ) + .toString() + .split('?') + .first; + final res = json + .decode(utf8.decode((await http.get(Uri.parse(url))).bodyBytes)); + if (res['gateway'] == 'matrix' || + (res['unifiedpush'] is Map && + res['unifiedpush']['gateway'] == 'matrix')) { + endpoint = url; + } } } catch (e) { Logs().i( '[Push] No self-hosted unified push gateway present: ' + newEndpoint); } Logs().i('[Push] UnifiedPush using endpoint ' + endpoint); - final oldTokens = {}; - try { - final fcmToken = await firebase?.getToken(); - oldTokens.add(fcmToken); - } catch (_) {} await setupPusher( gatewayUrl: endpoint, - token: newEndpoint, - oldTokens: oldTokens, - useDeviceSpecificAppId: true, + token: Uri.tryParse(newEndpoint)?.queryParameters['token'], + useDeviceSpecificAppId: false, ); await store.setItem(SettingKeys.unifiedPushEndpoint, newEndpoint); await store.setItemBool(SettingKeys.unifiedPushRegistered, true); diff --git a/scripts/build-ios.sh b/scripts/build-ios.sh index 7fd791b5e..9ea2c8c91 100755 --- a/scripts/build-ios.sh +++ b/scripts/build-ios.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash -git apply ./scripts/enable-android-google-services.patch FLUFFYCHAT_ORIG_GROUP="im.fluffychat" FLUFFYCHAT_ORIG_TEAM="4NXF6Z997G" #FLUFFYCHAT_NEW_GROUP="com.example.fluffychat" diff --git a/scripts/code_analyze.sh b/scripts/code_analyze.sh index c0249a7c5..0adaeea1b 100755 --- a/scripts/code_analyze.sh +++ b/scripts/code_analyze.sh @@ -2,7 +2,5 @@ flutter pub get flutter pub run import_sorter:main --no-comments --exit-if-changed flutter format lib/ test/ --set-exit-if-changed -git apply ./scripts/enable-android-google-services.patch -flutter pub get flutter analyze flutter pub run dart_code_metrics:metrics lib -r gitlab > code-quality-report.json || true \ No newline at end of file diff --git a/scripts/enable-android-google-services.patch b/scripts/enable-android-google-services.patch deleted file mode 100644 index 665481f16..000000000 --- a/scripts/enable-android-google-services.patch +++ /dev/null @@ -1,91 +0,0 @@ -diff --git a/android/app/build.gradle b/android/app/build.gradle -index ad9ffb87..37baafb1 100644 ---- a/android/app/build.gradle -+++ b/android/app/build.gradle -@@ -44,7 +44,7 @@ android { - - defaultConfig { - applicationId "chat.fluffy.fluffychat" -- minSdkVersion 16 -+ minSdkVersion 19 - targetSdkVersion 30 - versionCode flutterVersionCode.toInteger() - versionName flutterVersionName -@@ -82,11 +82,11 @@ flutter { - - dependencies { - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" -- //implementation 'com.google.firebase:firebase-messaging:19.0.1' // Workaround for https://github.com/microg/android_packages_apps_GmsCore/issues/313#issuecomment-617651698 -+ implementation 'com.google.firebase:firebase-messaging:19.0.1' // Workaround for https://github.com/microg/android_packages_apps_GmsCore/issues/313#issuecomment-617651698 - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' - implementation 'androidx.multidex:multidex:2.0.1' - } - --//apply plugin: 'com.google.gms.google-services' -+apply plugin: 'com.google.gms.google-services' -diff --git a/android/app/src/main/kotlin/chat/fluffy/fluffychat/FcmPushService.kt b/android/app/src/main/kotlin/chat/fluffy/fluffychat/FcmPushService.kt -index d9930f55..510e9845 100644 ---- a/android/app/src/main/kotlin/chat/fluffy/fluffychat/FcmPushService.kt -+++ b/android/app/src/main/kotlin/chat/fluffy/fluffychat/FcmPushService.kt -@@ -1,4 +1,4 @@ --/*package chat.fluffy.fluffychat -+package chat.fluffy.fluffychat - - import com.famedly.fcm_shared_isolate.FcmSharedIsolateService - -@@ -33,4 +33,3 @@ class FcmPushService : FcmSharedIsolateService() { - } - } - } --*/ -\ No newline at end of file -diff --git a/android/build.gradle b/android/build.gradle -index 85aa8647..3b7e09e7 100644 ---- a/android/build.gradle -+++ b/android/build.gradle -@@ -8,7 +8,7 @@ buildscript { - dependencies { - classpath 'com.android.tools.build:gradle:4.2.1' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" -- //classpath 'com.google.gms:google-services:4.3.8' -+ classpath 'com.google.gms:google-services:4.3.8' - } - } - -diff --git a/lib/utils/background_push.dart b/lib/utils/background_push.dart -index 00ca2aaa..8bb8a156 100644 ---- a/lib/utils/background_push.dart -+++ b/lib/utils/background_push.dart -@@ -39,7 +39,7 @@ import '../config/setting_keys.dart'; - import 'famedlysdk_store.dart'; - import 'platform_infos.dart'; - --//import 'package:fcm_shared_isolate/fcm_shared_isolate.dart'; -+import 'package:fcm_shared_isolate/fcm_shared_isolate.dart'; - - class NoTokenException implements Exception { - String get cause => 'Cannot get firebase token'; -@@ -65,7 +65,7 @@ class BackgroundPush { - - final pendingTests = >{}; - -- final dynamic firebase = null; //FcmSharedIsolate(); -+ final dynamic firebase = FcmSharedIsolate(); - - DateTime? lastReceivedPush; - -diff --git a/pubspec.yaml b/pubspec.yaml -index c6295788..8dd17ce4 100644 ---- a/pubspec.yaml -+++ b/pubspec.yaml -@@ -24,7 +24,7 @@ dependencies: - email_validator: ^2.0.1 - emoji_picker_flutter: ^1.1.2 - encrypt: ^5.0.1 -- #fcm_shared_isolate: ^0.1.0 -+ fcm_shared_isolate: ^0.1.0 - file_picker_cross: ^4.5.0 - flutter: - sdk: flutter diff --git a/scripts/prepare-android-release.sh b/scripts/prepare-android-release.sh index 8204f7ca1..a2a11dcb1 100755 --- a/scripts/prepare-android-release.sh +++ b/scripts/prepare-android-release.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash -git apply ./scripts/enable-android-google-services.patch cd android echo $FDROID_KEY | base64 --decode --ignore-garbage > key.jks echo "storePassword=${FDROID_KEY_PASS}" >> key.properties diff --git a/scripts/release-ios-testflight.sh b/scripts/release-ios-testflight.sh index f536702ce..fc241f5c9 100755 --- a/scripts/release-ios-testflight.sh +++ b/scripts/release-ios-testflight.sh @@ -1,5 +1,4 @@ #!/bin/sh -ve -git apply ./scripts/enable-android-google-services.patch flutter clean flutter pub get cd ios