Skip to content

Commit

Permalink
feat: Switch to firebase messaging package
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Mar 9, 2024
1 parent 3ca8bf6 commit b3d5bca
Show file tree
Hide file tree
Showing 23 changed files with 114 additions and 257 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: dart run license_checker check-licenses -c licenses.yaml --problematic
- run: flutter analyze
- name: Apply google services patch
run: git apply ./scripts/enable-android-google-services.patch
run: ./scripts/enable-google-services.sh
- run: flutter analyze
- run: flutter test

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Install Fastlane
run: gem install fastlane -NV
- name: Apply Google Services Patch
run: git apply ./scripts/enable-android-google-services.patch
run: ./scripts/enable-google-services.sh
- name: Remove Emoji Font
run: |
rm -rf fonts/NotoEmoji
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
flutter-version: ${{ env.FLUTTER_VERSION }}
cache: true
- name: Apply Google Services Patch
run: git apply ./scripts/enable-android-google-services.patch
run: ./scripts/enable-google-services.sh
- name: Remove Emoji Font
run: |
rm -rf fonts/NotoEmoji
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:
- name: Install Fastlane
run: gem install fastlane -NV
- name: Apply Google Services Patch
run: git apply ./scripts/enable-android-google-services.patch
run: ./scripts/enable-google-services.sh
- name: Remove Emoji Font
run: |
rm -rf fonts/NotoEmoji
Expand Down
5 changes: 2 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ android {
}
release {
signingConfig signingConfigs.release
}
}
}
// https://stackoverflow.com/a/77494454/8222484
packagingOptions {
Expand All @@ -86,8 +86,7 @@ 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 'androidx.multidex:multidex:2.0.1'
}

//apply plugin: 'com.google.gms.google-services'
//<GOOGLE_SERVICES>apply plugin: 'com.google.gms.google-services'
7 changes: 0 additions & 7 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@
</intent-filter>
</activity>

<service android:name=".FcmPushService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
android:foregroundServiceType="camera|microphone|mediaProjection">
</service>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import android.content.Context
import androidx.multidex.MultiDex

class MainActivity : FlutterActivity() {

override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}


override fun provideFlutterEngine(context: Context): FlutterEngine? {
return provideEngine(this)
}
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//classpath 'com.google.gms:google-services:4.3.8'
//<GOOGLE_SERVICES>classpath 'com.google.gms:google-services:4.3.8'
}
}

Expand Down
1 change: 1 addition & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"flutter":{"platforms":{"android":{"default":{"projectId":"fluffychat-ef3e8","appId":"1:865731724731:android:ec427b3b1dcd4a1e64309e","fileOutput":"android/app/google-services.json"}},"ios":{"default":{"projectId":"fluffychat-ef3e8","appId":"1:865731724731:ios:79fd983ce46cb40c64309e","uploadDebugSymbols":false,"fileOutput":"ios/Runner/GoogleService-Info.plist"}},"macos":{"default":{"projectId":"fluffychat-ef3e8","appId":"1:865731724731:ios:6fb777cf513cdb6264309e","uploadDebugSymbols":false,"fileOutput":"macos/Runner/GoogleService-Info.plist"}},"dart":{"lib/firebase_options.dart":{"projectId":"fluffychat-ef3e8","configurations":{"android":"1:865731724731:android:ec427b3b1dcd4a1e64309e","ios":"1:865731724731:ios:79fd983ce46cb40c64309e","macos":"1:865731724731:ios:6fb777cf513cdb6264309e","web":"1:865731724731:web:d367990bc625c24864309e"}}}}}}
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//<GOOGLE_SERVICES>import 'package:firebase_core/firebase_core.dart';

import 'package:flutter/material.dart';

import 'package:collection/collection.dart';
Expand All @@ -21,6 +23,8 @@ void main() async {
// widget bindings are initialized already.
WidgetsFlutterBinding.ensureInitialized();

//<GOOGLE_SERVICES>await Firebase.initializeApp();

Logs().nativeColors = !PlatformInfos.isIOS;
final store = await SharedPreferences.getInstance();
final clients = await ClientManager.getClients(store: store);
Expand Down
26 changes: 13 additions & 13 deletions lib/utils/background_push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import '../config/setting_keys.dart';
import '../widgets/matrix.dart';
import 'platform_infos.dart';

//import 'package:fcm_shared_isolate/fcm_shared_isolate.dart';
//<GOOGLE_SERVICES>import 'package:firebase_messaging/firebase_messaging.dart';

class NoTokenException implements Exception {
String get cause => 'Cannot get firebase token';
Expand All @@ -63,24 +63,23 @@ class BackgroundPush {

final pendingTests = <String, Completer<void>>{};

final dynamic firebase = null; //FcmSharedIsolate();
//<GOOGLE_SERVICES>final firebase = FirebaseMessaging.instance;

DateTime? lastReceivedPush;

bool upAction = false;

BackgroundPush._(this.client) {
firebase?.setListeners(
onMessage: (message) => pushHelper(
PushNotification.fromJson(
Map<String, dynamic>.from(message['data'] ?? message),
),
void onMessage(message) => pushHelper(
PushNotification.fromJson(Map<String, dynamic>.from(message.data)),
client: client,
l10n: l10n,
activeRoomId: matrix?.activeRoomId,
onSelectNotification: goToRoom,
),
);
);

BackgroundPush._(this.client) {
//<GOOGLE_SERVICES>FirebaseMessaging.onMessage.listen(onMessage);
//<GOOGLE_SERVICES>FirebaseMessaging.onBackgroundMessage(pushHelperBackground);
if (Platform.isAndroid) {
UnifiedPush.initialize(
onNewEndpoint: _newUpEndpoint,
Expand Down Expand Up @@ -132,7 +131,7 @@ class BackgroundPush {
bool useDeviceSpecificAppId = false,
}) async {
if (PlatformInfos.isIOS) {
await firebase?.requestPermission();
//<GOOGLE_SERVICES> await firebase.requestPermission();
} else if (PlatformInfos.isAndroid) {
_flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
Expand Down Expand Up @@ -289,7 +288,7 @@ class BackgroundPush {
Logs().v('Setup firebase');
if (_fcmToken?.isEmpty ?? true) {
try {
_fcmToken = await firebase?.getToken();
//<GOOGLE_SERVICES>_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);
Expand Down Expand Up @@ -358,7 +357,8 @@ class BackgroundPush {
Logs().i('[Push] UnifiedPush using endpoint $endpoint');
final oldTokens = <String?>{};
try {
final fcmToken = await firebase?.getToken();
String? fcmToken;
//<GOOGLE_SERVICES>fcmToken = await firebase.getToken();
oldTokens.add(fcmToken);
} catch (_) {}
await setupPusher(
Expand Down
39 changes: 21 additions & 18 deletions lib/utils/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart';
abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
static Future<List<Client>> getClients({
bool initialize = true,
bool isBackgroundClient = false,
required SharedPreferences store,
}) async {
if (PlatformInfos.isLinux) {
Expand All @@ -44,23 +44,26 @@ abstract class ClientManager {
await store.setStringList(clientNamespace, clientNames.toList());
}
final clients = clientNames.map(createClient).toList();
if (initialize) {
await Future.wait(
clients.map(
(client) => client.initWithRestore(
onMigration: () {
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
sendInitNotification(
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
);
},
).catchError(
(e, s) => Logs().e('Unable to initialize client', e, s),
),
),
);
}

await Future.wait(
clients.map(
(client) => client
.initWithRestore(
isBackgroundClient: isBackgroundClient,
onMigration: () {
final l10n = lookupL10n(PlatformDispatcher.instance.locale);
sendInitNotification(
l10n.databaseMigrationTitle,
l10n.databaseMigrationBody,
);
},
)
.catchError(
(e, s) => Logs().e('Unable to initialize client', e, s),
),
),
);

if (clients.length > 1 && clients.any((c) => !c.isLogged())) {
final loggedOutClients = clients.where((c) => !c.isLogged()).toList();
for (final client in loggedOutClients) {
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/init_with_restore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ extension InitWithRestoreExtension on Client {
);
}

Future<void> initWithRestore({void Function()? onMigration}) async {
Future<void> initWithRestore({
bool isBackgroundClient = false,
void Function()? onMigration,
}) async {
final storageKey =
'${AppConfig.applicationName}_session_backup_$clientName';
final storage = PlatformInfos.isMobile || PlatformInfos.isLinux
Expand All @@ -73,6 +76,7 @@ extension InitWithRestoreExtension on Client {
onMigration: onMigration,
waitForFirstSync: false,
waitUntilLoadCompletedLoaded: false,
isBackgroundClient: isBackgroundClient,
);
if (isLogged()) {
final accessToken = this.accessToken;
Expand Down
12 changes: 9 additions & 3 deletions lib/utils/push_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/voip/callkeep_manager.dart';

Future<void> pushHelperBackground(message) => pushHelper(
PushNotification.fromJson(message.data),
);

Future<void> pushHelper(
PushNotification notification, {
Client? client,
Expand Down Expand Up @@ -53,7 +57,7 @@ Future<void> pushHelper(
l10n.newMessageInFluffyChat,
l10n.openAppToReadMessages,
NotificationDetails(
iOS: const DarwinNotificationDetails(),
iOS: const DarwinNotificationDetails(sound: 'notification.caf'),
android: AndroidNotificationDetails(
AppConfig.pushNotificationsChannelId,
l10n.incomingMessages,
Expand Down Expand Up @@ -101,7 +105,7 @@ Future<void> _tryPushHelper(
);

client ??= (await ClientManager.getClients(
initialize: false,
isBackgroundClient: true,
store: await SharedPreferences.getInstance(),
))
.first;
Expand Down Expand Up @@ -282,7 +286,9 @@ Future<void> _tryPushHelper(
priority: Priority.max,
groupKey: notificationGroupId,
);
const iOSPlatformChannelSpecifics = DarwinNotificationDetails();
const iOSPlatformChannelSpecifics = DarwinNotificationDetails(
sound: "notification.caf",
);
final platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
Expand Down
4 changes: 4 additions & 0 deletions macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F22044A3C60003C045 /* Assets.xcassets */; };
33CC10F62044A3C60003C045 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F42044A3C60003C045 /* MainMenu.xib */; };
33CC11132044BFA00003C045 /* MainFlutterWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC11122044BFA00003C045 /* MainFlutterWindow.swift */; };
3AF44CC3D61A8332019AFCDE /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5AEFA815523969D4ADFBE7E8 /* GoogleService-Info.plist */; };
9CAF203E1D098383F2EDFFCB /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B3C62FEBAA272B5A33AFFC95 /* Pods_Runner.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -69,6 +70,7 @@
33E51914231749380026EE4D /* Release.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = Release.entitlements; sourceTree = "<group>"; };
33E5194F232828860026EE4D /* AppInfo.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = AppInfo.xcconfig; sourceTree = "<group>"; };
35E6B919318905352ECC7D69 /* Pods-RunnerTests.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.profile.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.profile.xcconfig"; sourceTree = "<group>"; };
5AEFA815523969D4ADFBE7E8 /* GoogleService-Info.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "Runner/GoogleService-Info.plist"; sourceTree = "<group>"; };
5CDC3DD55F4AC23D2067B292 /* Pods-RunnerTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.debug.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.debug.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
Expand Down Expand Up @@ -110,6 +112,7 @@
33CC10EE2044A3C60003C045 /* Products */,
D73912EC22F37F3D000D13A0 /* Frameworks */,
F9F203356080D460FB6D4567 /* Pods */,
5AEFA815523969D4ADFBE7E8 /* GoogleService-Info.plist */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -255,6 +258,7 @@
files = (
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */,
33CC10F62044A3C60003C045 /* MainMenu.xib in Resources */,
3AF44CC3D61A8332019AFCDE /* GoogleService-Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions macos/Runner/GoogleService-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>API_KEY</key>
<string>AIzaSyA8ZUBcuny0HjPwF2Q2fvDyQTC5dG2VHlE</string>
<key>GCM_SENDER_ID</key>
<string>865731724731</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>im.fluffychat.fluffychat</string>
<key>PROJECT_ID</key>
<string>fluffychat-ef3e8</string>
<key>STORAGE_BUCKET</key>
<string>fluffychat-ef3e8.appspot.com</string>
<key>IS_ADS_ENABLED</key>
<false></false>
<key>IS_ANALYTICS_ENABLED</key>
<false></false>
<key>IS_APPINVITE_ENABLED</key>
<true></true>
<key>IS_GCM_ENABLED</key>
<true></true>
<key>IS_SIGNIN_ENABLED</key>
<true></true>
<key>GOOGLE_APP_ID</key>
<string>1:865731724731:ios:6fb777cf513cdb6264309e</string>
</dict>
</plist>
Loading

0 comments on commit b3d5bca

Please sign in to comment.