Prep flutter for android data-messages
This commit is contained in:
@@ -12,6 +12,7 @@ import 'package:simplecloudnotifier/nav_layout.dart';
|
||||
import 'package:simplecloudnotifier/state/app_bar_state.dart';
|
||||
import 'package:simplecloudnotifier/state/app_theme.dart';
|
||||
import 'package:simplecloudnotifier/state/application_log.dart';
|
||||
import 'package:simplecloudnotifier/state/fb_message.dart';
|
||||
import 'package:simplecloudnotifier/state/globals.dart';
|
||||
import 'package:simplecloudnotifier/state/request_log.dart';
|
||||
import 'package:simplecloudnotifier/state/app_auth.dart';
|
||||
@@ -40,6 +41,7 @@ void main() async {
|
||||
Hive.registerAdapter(SCNLogLevelAdapter());
|
||||
Hive.registerAdapter(MessageAdapter());
|
||||
Hive.registerAdapter(ChannelAdapter());
|
||||
Hive.registerAdapter(FBMessageAdapter());
|
||||
|
||||
print('[INIT] Load Hive<scn-requests>...');
|
||||
|
||||
@@ -81,6 +83,16 @@ void main() async {
|
||||
ApplicationLog.error('Failed to open Hive-Box: scn-channel-cache' + exc.toString(), trace: trace);
|
||||
}
|
||||
|
||||
print('[INIT] Load Hive<scn-fb-messages>...');
|
||||
|
||||
try {
|
||||
await Hive.openBox<FBMessage>('scn-fb-messages');
|
||||
} catch (exc, trace) {
|
||||
Hive.deleteBoxFromDisk('scn-fb-messages');
|
||||
await Hive.openBox<FBMessage>('scn-fb-messages');
|
||||
ApplicationLog.error('Failed to open Hive-Box: scn-fb-messages' + exc.toString(), trace: trace);
|
||||
}
|
||||
|
||||
print('[INIT] Load AppAuth...');
|
||||
|
||||
final appAuth = AppAuth(); // ensure UserAccount is loaded
|
||||
@@ -128,6 +140,9 @@ void main() async {
|
||||
} catch (exc, trace) {
|
||||
ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace);
|
||||
}
|
||||
|
||||
FirebaseMessaging.onBackgroundMessage(_onBackgroundMessage);
|
||||
FirebaseMessaging.onMessage.listen(_onForegroundMessage);
|
||||
} else {
|
||||
print('[INIT] Skip Firebase init (Platform == Linux)...');
|
||||
}
|
||||
@@ -146,6 +161,33 @@ void main() async {
|
||||
);
|
||||
}
|
||||
|
||||
class SCNApp extends StatelessWidget {
|
||||
SCNApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ToastificationWrapper(
|
||||
config: ToastificationConfig(
|
||||
itemWidth: 440,
|
||||
marginBuilder: (alignment) => EdgeInsets.symmetric(vertical: 64),
|
||||
animationDuration: Duration(milliseconds: 200),
|
||||
),
|
||||
child: Consumer<AppTheme>(
|
||||
builder: (context, appTheme, child) => MaterialApp(
|
||||
title: 'SimpleCloudNotifier',
|
||||
navigatorObservers: [Navi.routeObserver, Navi.modalRouteObserver],
|
||||
theme: ThemeData(
|
||||
//TODO color settings
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: appTheme.darkMode ? Brightness.dark : Brightness.light),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: SCNNavLayout(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void setFirebaseToken(String fcmToken) async {
|
||||
final acc = AppAuth();
|
||||
|
||||
@@ -182,29 +224,18 @@ void setFirebaseToken(String fcmToken) async {
|
||||
}
|
||||
}
|
||||
|
||||
class SCNApp extends StatelessWidget {
|
||||
SCNApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ToastificationWrapper(
|
||||
config: ToastificationConfig(
|
||||
itemWidth: 440,
|
||||
marginBuilder: (alignment) => EdgeInsets.symmetric(vertical: 64),
|
||||
animationDuration: Duration(milliseconds: 200),
|
||||
),
|
||||
child: Consumer<AppTheme>(
|
||||
builder: (context, appTheme, child) => MaterialApp(
|
||||
title: 'SimpleCloudNotifier',
|
||||
navigatorObservers: [Navi.routeObserver, Navi.modalRouteObserver],
|
||||
theme: ThemeData(
|
||||
//TODO color settings
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: appTheme.darkMode ? Brightness.dark : Brightness.light),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: SCNNavLayout(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Future<void> _onBackgroundMessage(RemoteMessage message) async {
|
||||
await _receiveMessage(message, false);
|
||||
}
|
||||
|
||||
void _onForegroundMessage(RemoteMessage message) {
|
||||
_receiveMessage(message, true);
|
||||
}
|
||||
|
||||
Future<void> _receiveMessage(RemoteMessage message, bool foreground) async {
|
||||
// ensure init
|
||||
Hive.openBox<SCNLog>('scn-logs');
|
||||
|
||||
ApplicationLog.info('Received FB message (${foreground ? 'foreground' : 'background'}): ${message.messageId ?? 'NULL'}');
|
||||
FBMessageLog.insert(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user