Fix linux build (does not support fcm)

This commit is contained in:
Mike Schwörer 2024-06-04 08:20:28 +02:00
parent 8e0c8e825b
commit 549311535c
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 64 additions and 25 deletions

View File

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
@ -17,15 +19,24 @@ import 'firebase_options.dart';
void main() async {
print('[INIT] Application starting...');
print('[INIT] Ensure WidgetsFlutterBinding...');
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
print('[INIT] Init Globals...');
await Globals().init();
print('[INIT] Init Hive...');
await Hive.initFlutter();
Hive.registerAdapter(SCNRequestAdapter());
Hive.registerAdapter(SCNLogAdapter());
Hive.registerAdapter(SCNLogLevelAdapter());
print('[INIT] Load Hive<scn-requests>...');
try {
await Hive.openBox<SCNRequest>('scn-requests');
} catch (exc, trace) {
@ -34,6 +45,8 @@ void main() async {
ApplicationLog.error('Failed to open Hive-Box: scn-requests: ' + exc.toString(), trace: trace);
}
print('[INIT] Load Hive<scn-logs>...');
try {
await Hive.openBox<SCNLog>('scn-logs');
} catch (exc, trace) {
@ -42,45 +55,58 @@ void main() async {
ApplicationLog.error('Failed to open Hive-Box: scn-logs: ' + exc.toString(), trace: trace);
}
print('[INIT] Load AppAuth...');
final appAuth = AppAuth(); // ensure UserAccount is loaded
if (appAuth.isAuth()) {
try {
print('[INIT] Load User...');
await appAuth.loadUser();
//TODO fallback to cached user (perhaps event use cached client (if exists) directly and only update/load in background)
} catch (exc, trace) {
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
}
try {
print('[INIT] Load Client...');
await appAuth.loadClient();
//TODO fallback to cached client (perhaps event use cached client (if exists) directly and only update/load in background)
} catch (exc, trace) {
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
}
}
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
if (!Platform.isLinux) {
print('[INIT] Init Firebase...');
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseMessaging.instance.requestPermission(provisional: true);
print('[INIT] Request Notification permissions...');
await FirebaseMessaging.instance.requestPermission(provisional: true);
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
try {
setFirebaseToken(fcmToken);
} catch (exc, trace) {
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
}
}).onError((dynamic err) {
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
});
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
try {
setFirebaseToken(fcmToken);
print('[INIT] Query firebase token...');
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken != null) {
setFirebaseToken(fcmToken);
}
} catch (exc, trace) {
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace);
}
}).onError((dynamic err) {
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
});
try {
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken != null) {
setFirebaseToken(fcmToken);
}
} catch (exc, trace) {
ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace);
} else {
print('[INIT] Skip Firebase init (Platform == Linux)...');
}
ApplicationLog.debug('Application started');
ApplicationLog.debug('[INIT] Application started');
runApp(
MultiProvider(

View File

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
@ -9,6 +11,7 @@ import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';
import 'package:uuid/uuid.dart';
class AccountRootPage extends StatefulWidget {
const AccountRootPage({super.key});
@ -447,15 +450,21 @@ class _AccountRootPageState extends State<AccountRootPage> {
final acc = Provider.of<AppAuth>(context, listen: false);
try {
final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true);
final String? fcmToken;
if (Platform.isLinux) {
Toaster.warn("Unsupported Platform", 'Your platform is not supported by FCM - notifications will not work');
fcmToken = '(linux-' + Uuid().v4() + ')';
} else {
final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true);
if (notificationSettings.authorizationStatus == AuthorizationStatus.denied) {
Toaster.error("Missing Permission", 'Please allow notifications to create an account');
return;
if (notificationSettings.authorizationStatus == AuthorizationStatus.denied) {
Toaster.error("Missing Permission", 'Please allow notifications to create an account');
return;
}
fcmToken = await FirebaseMessaging.instance.getToken();
}
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken == null) {
Toaster.warn("Missing Token", 'No FCM Token found, please allow notifications, ensure you have a network connection and restart the app');
return;

View File

@ -12,6 +12,9 @@ import 'package:simplecloudnotifier/pages/message_list/message_list_item.dart';
class MessageListPage extends StatefulWidget {
const MessageListPage({super.key});
//TODO reload on switch to tab
//TODO reload on app to foreground
@override
State<MessageListPage> createState() => _MessageListPageState();
}

View File

@ -941,7 +941,7 @@ packages:
source: hosted
version: "3.1.1"
uuid:
dependency: transitive
dependency: "direct main"
description:
name: uuid
sha256: "814e9e88f21a176ae1359149021870e87f7cddaf633ab678a5d2b0bff7fd1ba8"

View File

@ -29,6 +29,7 @@ dependencies:
firebase_messaging: ^14.9.4
device_info_plus: ^10.1.0
toastification: ^2.0.0
uuid: ^4.4.0
dependency_overrides: