diff --git a/lib/main.dart b/lib/main.dart index 654bbc3..f44ad61 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,9 @@ import 'package:camera/camera.dart'; -import 'package:flutter/foundation.dart'; import 'package:provider/provider.dart'; import 'package:twonly/globals.dart'; import 'package:twonly/src/database/twonly_database.dart'; import 'package:twonly/src/providers/api_provider.dart'; import 'package:flutter/material.dart'; -import 'package:logging/logging.dart'; import 'package:twonly/src/providers/hive.dart'; import 'package:twonly/src/providers/settings_change_provider.dart'; import 'package:twonly/src/services/fcm_service.dart'; @@ -22,14 +20,7 @@ void main() async { // This prevents a sudden theme change when the app is first displayed. await settingsController.loadSettings(); - Logger.root.level = kReleaseMode ? Level.INFO : Level.ALL; - Logger.root.onRecord.listen((record) { - writeLogToFile(record); - if (kDebugMode) { - print( - '${record.level.name}: twonly:${record.loggerName}: ${record.message}'); - } - }); + setupLogger(); await setupPushNotification(); await initMediaStorage(); diff --git a/lib/src/services/fcm_service.dart b/lib/src/services/fcm_service.dart index 2ca3fb5..55b431d 100644 --- a/lib/src/services/fcm_service.dart +++ b/lib/src/services/fcm_service.dart @@ -3,6 +3,7 @@ import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:logging/logging.dart'; import 'package:twonly/globals.dart'; import 'package:twonly/src/app.dart'; +import 'package:twonly/src/database/twonly_database.dart'; import 'package:twonly/src/services/notification_service.dart'; import 'package:twonly/src/utils/misc.dart'; import 'dart:io' show Platform; @@ -70,36 +71,34 @@ Future initFCMService() async { } FirebaseMessaging.onMessage.listen((RemoteMessage message) { - if (!Platform.isAndroid) { - Logger("firebase-notification").shout("Got message in Dart while on iOS"); - } - Logger("firebase-notification") .finer('Got a message while in the foreground!'); - - print('Message data: ${message.data}'); - - if (message.notification != null) { - print('Message also contained a notification: ${message.notification}'); - String title = message.notification!.title ?? ""; - String body = message.notification!.body ?? ""; - customLocalPushNotification(title, body); - } else if (message.data["push_data"] != null) { - handlePushData(message.data["push_data"]); - } + handleRemoteMessage(message); }); } @pragma('vm:entry-point') Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { + setupLogger(); Logger("firebase-background") .shout('Handling a background message: ${message.messageId}'); + twonlyDatabase = TwonlyDatabase(); + await handleRemoteMessage(message); + // make sure every thing run... + await Future.delayed(Duration(milliseconds: 2000)); +} + +Future handleRemoteMessage(RemoteMessage message) async { if (!Platform.isAndroid) { Logger("firebase-notification").shout("Got message in Dart while on iOS"); } - Logger("firebase-notification") - .finer('Got a message while in the background!'); - print('Message data: ${message.data}'); + if (message.notification != null) { + String title = message.notification!.title ?? ""; + String body = message.notification!.body ?? ""; + await customLocalPushNotification(title, body); + } else if (message.data["push_data"] != null) { + await handlePushData(message.data["push_data"]); + } } diff --git a/lib/src/services/notification_service.dart b/lib/src/services/notification_service.dart index 5cd1ac5..67e5cf8 100644 --- a/lib/src/services/notification_service.dart +++ b/lib/src/services/notification_service.dart @@ -309,13 +309,13 @@ Future handlePushData(String pushDataJson) async { if (pushKind != null) { if (pushKind == PushKind.testNotification) { - customLocalPushNotification( + await customLocalPushNotification( "Test notification", "This is a test notification."); } else if (fromUserId != null) { - showLocalPushNotification(fromUserId, pushKind); + await showLocalPushNotification(fromUserId, pushKind); } else { - showLocalPushNotificationWithoutUserId(pushKind); - setupNotificationWithUsers(); + await showLocalPushNotificationWithoutUserId(pushKind); + await setupNotificationWithUsers(); } } } catch (e) { diff --git a/lib/src/utils/misc.dart b/lib/src/utils/misc.dart index 1475188..51c36db 100644 --- a/lib/src/utils/misc.dart +++ b/lib/src/utils/misc.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'dart:math'; import 'package:connectivity_plus/connectivity_plus.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_image_compress/flutter_image_compress.dart'; @@ -202,3 +203,14 @@ PieTheme getPieCanvasTheme(BuildContext context) { ), ); } + +void setupLogger() { + Logger.root.level = kReleaseMode ? Level.INFO : Level.ALL; + Logger.root.onRecord.listen((record) async { + await writeLogToFile(record); + if (kDebugMode) { + print( + '${record.level.name}: twonly:${record.loggerName}: ${record.message}'); + } + }); +} diff --git a/pubspec.yaml b/pubspec.yaml index 05339f9..8218f75 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ description: "Send pictures to friends in real time and be sure you are the only # Prevent accidental publishing to pub.dev. publish_to: 'none' -version: 0.0.16+16 +version: 0.0.19+19 environment: sdk: ^3.6.0