Fix: Issue with background notifications on Android
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run

This commit is contained in:
otsmr 2026-06-08 22:05:22 +02:00
parent b7b2ad701f
commit 053fbeb66e
8 changed files with 290 additions and 231 deletions

View file

@ -1,5 +1,9 @@
# Changelog
## 0.2.31
- Fix: Issue with background notifications on Android
## 0.2.30
- Fix: Changed minimum threshold for the user discovery to 3

View file

@ -76,7 +76,7 @@ void main() async {
unawaited(StartupGuard.markAppStartup());
var storageError = await twonlyMinimumInitialization();
await initFCMService();
await FcmNotificationService.initStartup();
var userExists = false;
@ -109,6 +109,8 @@ void main() async {
unawaited(initFileDownloader());
if (userExists) {
unawaited(FcmNotificationService.initAfterUserLoaded());
if (userService.currentUser.allowErrorTrackingViaSentry) {
AppState.allowErrorTrackingViaSentry = true;
await SentryFlutter.init(

View file

@ -21,7 +21,8 @@ import 'package:twonly/locator.dart';
import 'package:twonly/src/database/twonly.db.dart';
import 'package:twonly/src/model/protobuf/api/websocket/client_to_server.pbserver.dart';
import 'package:twonly/src/model/protobuf/api/websocket/error.pb.dart';
import 'package:twonly/src/model/protobuf/api/websocket/server_to_client.pb.dart' as server;
import 'package:twonly/src/model/protobuf/api/websocket/server_to_client.pb.dart'
as server;
import 'package:twonly/src/model/protobuf/api/websocket/server_to_client.pbserver.dart';
import 'package:twonly/src/services/api/client2client/user_discovery.c2c.dart';
import 'package:twonly/src/services/api/mediafiles/download.api.dart';
@ -65,13 +66,15 @@ class ApiService {
Stream<SubscriptionPlan> get onPlanUpdated => _planUpdateController.stream;
final _connectionStateController = StreamController<bool>.broadcast();
Stream<bool> get onConnectionStateUpdated => _connectionStateController.stream;
Stream<bool> get onConnectionStateUpdated =>
_connectionStateController.stream;
final _appOutdatedController = StreamController<void>.broadcast();
Stream<void> get onAppOutdated => _appOutdatedController.stream;
final _newDeviceRegisteredController = StreamController<void>.broadcast();
Stream<void> get onNewDeviceRegistered => _newDeviceRegisteredController.stream;
Stream<void> get onNewDeviceRegistered =>
_newDeviceRegisteredController.stream;
bool appIsOutdated = false;
bool isAuthenticated = false;
@ -80,7 +83,8 @@ class ApiService {
Timer? reconnectionTimer;
int _reconnectionDelay = 5;
final HashMap<Int64, Completer<server.ServerToClient?>> _pendingRequests = HashMap();
final HashMap<Int64, Completer<server.ServerToClient?>> _pendingRequests =
HashMap();
IOWebSocketChannel? _channel;
// ignore: cancel_subscriptions
StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;
@ -112,7 +116,7 @@ class ApiService {
// Function is called after the user is authenticated at the server
Future<void> onAuthenticated() async {
await initFCMAfterAuthenticated();
await FcmNotificationService.initFCMAfterAuthenticated();
_connectionStateController.add(true);
if (AppState.isInBackgroundTask) {
@ -418,7 +422,9 @@ class ApiService {
}
if (res.error == ErrorCode.UserIdNotFound && contactId != null) {
Log.warn('Contact deleted their account $contactId.');
final contact = await twonlyDB.contactsDao.getContactByUserId(contactId).getSingleOrNull();
final contact = await twonlyDB.contactsDao
.getContactByUserId(contactId)
.getSingleOrNull();
if (contact != null) {
await twonlyDB.contactsDao.updateContact(
contactId,
@ -483,7 +489,8 @@ class ApiService {
return true;
}
if (result.isError) {
if (result.error != ErrorCode.AuthTokenNotValid && result.error != ErrorCode.ForegroundSessionConnected) {
if (result.error != ErrorCode.AuthTokenNotValid &&
result.error != ErrorCode.ForegroundSessionConnected) {
Log.error(
'got error while authenticating to the server: ${result.error}',
);
@ -521,7 +528,8 @@ class ApiService {
return true;
}
if (result.isError) {
if (result.error != ErrorCode.AuthTokenNotValid && result.error != ErrorCode.ForegroundSessionConnected) {
if (result.error != ErrorCode.AuthTokenNotValid &&
result.error != ErrorCode.ForegroundSessionConnected) {
Log.error(
'got error while authenticating to the server: ${result.error}',
);
@ -553,7 +561,8 @@ class ApiService {
return;
}
final handshake = Handshake()..getAuthChallenge = Handshake_GetAuthChallenge();
final handshake = Handshake()
..getAuthChallenge = Handshake_GetAuthChallenge();
final req = createClientToServerFromHandshake(handshake);
final result = await sendRequestSync(req, authenticated: false);
@ -618,7 +627,9 @@ class ApiService {
final register = Handshake_Register()
..username = username
..publicIdentityKey = (await signalStore.getIdentityKeyPair()).getPublicKey().serialize()
..publicIdentityKey = (await signalStore.getIdentityKeyPair())
.getPublicKey()
.serialize()
..registrationId = Int64(signalIdentity.registrationId)
..signedPrekey = signedPreKey.getKeyPair().publicKey.serialize()
..signedPrekeySignature = signedPreKey.signature

View file

@ -5,6 +5,7 @@ import 'package:clock/clock.dart';
import 'package:drift/drift.dart';
import 'package:hashlib/random.dart';
import 'package:mutex/mutex.dart';
import 'package:twonly/globals.dart';
import 'package:twonly/locator.dart';
import 'package:twonly/src/database/daos/contacts.dao.dart';
@ -281,7 +282,7 @@ Future<(EncryptedContent?, PlaintextContent?)> handleEncryptedMessageRaw(
Log.info('[$receiptId] Finished handleEncryptedMessage');
if (a == null && b == null) {
unawaited(updateLastServerMessageTimestamp());
unawaited(FcmNotificationService.updateLastServerMessageTimestamp());
if (Platform.isAndroid) {
// Message was handled without any error. Show push notification to the user for Android.
await showPushNotificationFromServerMessages(

View file

@ -0,0 +1,29 @@
import 'dart:async';
import 'dart:io' show Platform;
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:twonly/globals.dart';
import 'package:twonly/src/services/background/callback_dispatcher.background.dart';
import 'package:twonly/src/services/notifications/fcm.notifications.dart';
import 'package:twonly/src/services/notifications/setup.notifications.dart';
import 'package:twonly/src/utils/log.dart';
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
SentryWidgetsFlutterBinding.ensureInitialized();
await AppEnvironment.init();
final isInitialized = await initBackgroundExecution();
await setupPushNotification();
Log.info('Handling a background message: ${message.messageId}');
await FcmNotificationService.handleRemoteMessage(message);
if (Platform.isAndroid) {
if (isInitialized) {
await handlePeriodicTask(lastExecutionInSecondsLimit: 10);
}
} else {
// make sure every thing run...
await Future.delayed(const Duration(milliseconds: 2000));
}
}

View file

@ -1,5 +1,3 @@
// ignore_for_file: unreachable_from_main
import 'dart:async';
import 'dart:io' show Platform;
@ -7,13 +5,11 @@ import 'package:firebase_app_installations/firebase_app_installations.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:twonly/globals.dart';
import 'package:twonly/locator.dart';
import 'package:twonly/src/constants/secure_storage.keys.dart';
import 'package:twonly/src/services/background/callback_dispatcher.background.dart';
import 'package:twonly/src/services/notifications/background.notifications.dart';
import 'package:twonly/src/services/notifications/setup.notifications.dart';
import 'package:twonly/src/services/notifications/fcm.background.dart';
import 'package:twonly/src/services/user.service.dart';
import 'package:twonly/src/utils/log.dart';
@ -21,9 +17,62 @@ import '../../../firebase_options.dart';
// see more here: https://firebase.google.com/docs/cloud-messaging/flutter/receive?hl=de
Future<void> checkForTokenUpdates() async {
class FcmNotificationService {
static Future<void> initStartup() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen(handleRemoteMessage);
}
static Future<void> initAfterUserLoaded() async {
unawaited(_checkForTokenUpdates());
unawaited(_checkFcmHealthAndResetIfNeeded());
}
static Future<void> initFCMAfterAuthenticated({bool force = false}) async {
final fcmToken = userService.currentUser.fcmToken;
if (userService.currentUser.updateFCMToken || force) {
if (fcmToken == null) {
Log.error('FCM token could not be updated as it is empty');
await _checkForTokenUpdates();
return;
}
final res = await apiService.updateFCMToken(
fcmToken,
);
if (res.isSuccess) {
Log.info('Uploaded new FCM token!');
await UserService.update((u) {
u.updateFCMToken = false;
});
} else {
Log.error('Could not update FCM token!');
}
}
}
static Future<void> resetFCMTokens() async {
await FirebaseInstallations.instance.delete();
Log.info('Firebase Installation successfully deleted.');
await FirebaseMessaging.instance.deleteToken();
Log.info('Old FCM deleted.');
await UserService.update((u) => u.fcmToken = null);
await _checkForTokenUpdates();
await initFCMAfterAuthenticated(force: true);
}
static Future<void> _checkForTokenUpdates() async {
try {
if (!userService.isUserCreated) return;
if (!userService.isUserCreated) {
Log.info(
'Checking for FCM token updates skipped: user is not yet created.',
);
return;
}
if (Platform.isIOS) {
var apnsToken = await FirebaseMessaging.instance.getAPNSToken();
for (var i = 0; i < 20; i++) {
@ -53,35 +102,8 @@ Future<void> checkForTokenUpdates() async {
..updateFCMToken = true
..fcmToken = fcmToken;
});
}
FirebaseMessaging.instance.onTokenRefresh
.listen((fcmToken) async {
await UserService.update((u) {
u
..updateFCMToken = true
..fcmToken = fcmToken;
});
})
.onError((err) {
Log.error('could not listen on token refresh');
});
} catch (e) {
Log.error('could not load fcm token: $e');
}
}
Future<void> initFCMAfterAuthenticated({bool force = false}) async {
final fcmToken = userService.currentUser.fcmToken;
if (userService.currentUser.updateFCMToken || force) {
if (fcmToken == null) {
Log.error('FCM token could not be updated as it is empty');
await checkForTokenUpdates();
return;
}
final res = await apiService.updateFCMToken(
fcmToken,
);
if (apiService.isAuthenticated) {
final res = await apiService.updateFCMToken(fcmToken);
if (res.isSuccess) {
Log.info('Uploaded new FCM token!');
await UserService.update((u) {
@ -93,50 +115,36 @@ Future<void> initFCMAfterAuthenticated({bool force = false}) async {
}
}
Future<void> resetFCMTokens() async {
await FirebaseInstallations.instance.delete();
Log.info('Firebase Installation successfully deleted.');
await FirebaseMessaging.instance.deleteToken();
Log.info('Old FCM deleted.');
await UserService.update((u) => u.fcmToken = null);
await checkForTokenUpdates();
await initFCMAfterAuthenticated(force: true);
}
Future<void> initFCMService() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
unawaited(checkForTokenUpdates());
unawaited(checkFcmHealthAndResetIfNeeded());
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen(handleRemoteMessage);
}
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
SentryWidgetsFlutterBinding.ensureInitialized();
await AppEnvironment.init();
final isInitialized = await initBackgroundExecution();
await setupPushNotification();
Log.info('Handling a background message: ${message.messageId}');
await handleRemoteMessage(message);
if (Platform.isAndroid) {
if (isInitialized) {
await handlePeriodicTask(lastExecutionInSecondsLimit: 10);
}
FirebaseMessaging.instance.onTokenRefresh
// ignore: avoid_types_on_closure_parameters
.listen((String fcmToken) async {
await UserService.update((u) {
u
..updateFCMToken = true
..fcmToken = fcmToken;
});
if (apiService.isAuthenticated) {
final res = await apiService.updateFCMToken(fcmToken);
if (res.isSuccess) {
Log.info('Uploaded new FCM token!');
await UserService.update((u) {
u.updateFCMToken = false;
});
} else {
// make sure every thing run...
await Future.delayed(const Duration(milliseconds: 2000));
Log.error('Could not update FCM token!');
}
}
})
.onError((err) {
Log.error('could not listen on token refresh');
});
} catch (e) {
Log.error('could not load fcm token: $e');
}
}
Future<void> handleRemoteMessage(RemoteMessage message) async {
await updateLastFcmMessageTimestamp();
static Future<void> handleRemoteMessage(RemoteMessage message) async {
await _updateLastFcmMessageTimestamp();
if (!Platform.isAndroid) {
Log.error('Got message in Dart while on iOS');
}
@ -154,15 +162,9 @@ Future<void> handleRemoteMessage(RemoteMessage message) async {
message.notification?.body ?? message.data['body'] as String? ?? '';
await customLocalPushNotification(title, body);
}
// On Android the push notification is now shown in the server_message.dart. This ensures
// that the messages was successfully decrypted before showing the push notification
// else if (message.data['push_data'] != null) {
// await handlePushData(message.data['push_data'] as String);
// }
}
Future<void> updateLastFcmMessageTimestamp() async {
static Future<void> _updateLastFcmMessageTimestamp() async {
const storage = FlutterSecureStorage();
final nowMs = DateTime.now().millisecondsSinceEpoch.toString();
try {
@ -180,7 +182,7 @@ Future<void> updateLastFcmMessageTimestamp() async {
}
}
Future<void> updateLastServerMessageTimestamp() async {
static Future<void> updateLastServerMessageTimestamp() async {
const storage = FlutterSecureStorage();
final nowMs = DateTime.now().millisecondsSinceEpoch.toString();
try {
@ -198,8 +200,11 @@ Future<void> updateLastServerMessageTimestamp() async {
}
}
Future<void> checkFcmHealthAndResetIfNeeded() async {
if (!userService.isUserCreated) return;
static Future<void> _checkFcmHealthAndResetIfNeeded() async {
if (!userService.isUserCreated) {
Log.info('FCM health check skipped: user is not yet created.');
return;
}
const storage = FlutterSecureStorage();
try {
final lastFcmStr = await storage.read(
@ -229,7 +234,9 @@ Future<void> checkFcmHealthAndResetIfNeeded() async {
}
if (lastFcmTime != null) {
Log.info('Last message received via FCM messaging system: $lastFcmTime');
Log.info(
'Last message received via FCM messaging system: $lastFcmTime',
);
} else {
Log.info('No record of a message received via FCM messaging system.');
}
@ -242,14 +249,15 @@ Future<void> checkFcmHealthAndResetIfNeeded() async {
}
}
// Check conditions:
// 1. No messages received via FCM in the last 3 days (either null or older than 3 days)
final fcmInactive = lastFcmTime == null || lastFcmTime.isBefore(threeDaysAgo);
// 2. Server message received within the last 3 days
final serverActive = lastServerTime != null && lastServerTime.isAfter(threeDaysAgo);
final fcmInactive =
lastFcmTime == null || lastFcmTime.isBefore(threeDaysAgo);
final serverActive =
lastServerTime != null && lastServerTime.isAfter(threeDaysAgo);
if (fcmInactive && serverActive) {
Log.warn('FCM has been inactive for >3 days, but server messages have been active. Resetting FCM tokens...');
Log.warn(
'FCM has been inactive for >3 days, but server messages have been active. Resetting FCM tokens...',
);
await resetFCMTokens();
} else {
Log.info('FCM check passed. No reset needed.');
@ -258,3 +266,4 @@ Future<void> checkFcmHealthAndResetIfNeeded() async {
Log.error('Error during FCM health check: $e');
}
}
}

View file

@ -10,6 +10,7 @@ import 'package:twonly/locator.dart';
import 'package:twonly/src/constants/routes.keys.dart';
import 'package:twonly/src/model/json/userdata.model.dart';
import 'package:twonly/src/model/protobuf/api/websocket/error.pb.dart';
import 'package:twonly/src/services/notifications/fcm.notifications.dart';
import 'package:twonly/src/services/signal/identity.signal.dart';
import 'package:twonly/src/services/user.service.dart';
import 'package:twonly/src/utils/log.dart';
@ -163,6 +164,8 @@ class _RegisterViewState extends State<RegisterView> {
await UserService.save(userData);
unawaited(FcmNotificationService.initAfterUserLoaded());
await apiService.authenticate();
widget.callbackOnSuccess();
} catch (e, stack) {

View file

@ -45,7 +45,7 @@ class _NotificationViewState extends State<NotificationView> {
_isLoadingTroubleshooting = true;
});
await initFCMAfterAuthenticated(force: true);
await FcmNotificationService.initFCMAfterAuthenticated(force: true);
await setupNotificationWithUsers(force: true);
@ -90,7 +90,7 @@ class _NotificationViewState extends State<NotificationView> {
setState(() {
_isLoadingReset = true;
});
await resetFCMTokens();
await FcmNotificationService.resetFCMTokens();
if (!mounted) return;
await showAlertDialog(
context,