forgot background notifications by android

This commit is contained in:
otsmr 2025-04-08 21:54:05 +02:00
parent 6c867ca8b5
commit b6a4cca884
5 changed files with 35 additions and 33 deletions

View file

@ -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();

View file

@ -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<void> _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"]);
}
}

View file

@ -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) {

View file

@ -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}');
}
});
}

View file

@ -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