mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-09-01 07:24:07 +00:00
try to connect first
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run
This commit is contained in:
parent
185ac49de9
commit
1f269ae5b3
3 changed files with 90 additions and 70 deletions
|
|
@ -89,7 +89,10 @@ Future<bool> initBackgroundExecution() async {
|
|||
final Mutex _keyValueMutex = Mutex();
|
||||
|
||||
// ignore: unreachable_from_main
|
||||
Future<void> handlePeriodicTask({int lastExecutionInSecondsLimit = 120}) async {
|
||||
Future<bool> backgroundFetch({
|
||||
int? lastExecutionInSecondsLimit = 120,
|
||||
}) async {
|
||||
if (lastExecutionInSecondsLimit != null) {
|
||||
final shouldBeExecuted = await exclusiveAccess(
|
||||
lockName: 'periodic_task',
|
||||
mutex: _keyValueMutex,
|
||||
|
|
@ -116,9 +119,10 @@ Future<void> handlePeriodicTask({int lastExecutionInSecondsLimit = 120}) async {
|
|||
},
|
||||
);
|
||||
|
||||
if (!shouldBeExecuted) return;
|
||||
if (!shouldBeExecuted) return false;
|
||||
}
|
||||
|
||||
Log.info('eu.twonly.periodic_task was called.');
|
||||
Log.info('Periodic task was called.');
|
||||
AppState.gotMessageFromServer = false;
|
||||
|
||||
final stopwatch = Stopwatch()..start();
|
||||
|
|
@ -130,14 +134,16 @@ Future<void> handlePeriodicTask({int lastExecutionInSecondsLimit = 120}) async {
|
|||
|
||||
if (!await apiService.connect()) {
|
||||
Log.info('Could not connect to the api. Returning early.');
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!apiService.isAuthenticated) {
|
||||
Log.info('Api is not authenticated. Returning early.');
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
var receiveMessage = false;
|
||||
|
||||
try {
|
||||
while (!AppState.gotMessageFromServer) {
|
||||
if (stopwatch.elapsed.inSeconds >= 15) {
|
||||
|
|
@ -148,19 +154,22 @@ Future<void> handlePeriodicTask({int lastExecutionInSecondsLimit = 120}) async {
|
|||
}
|
||||
|
||||
if (AppState.gotMessageFromServer) {
|
||||
receiveMessage = true;
|
||||
Log.info('Received a server message from the server.');
|
||||
}
|
||||
|
||||
await finishStartedPreprocessing();
|
||||
|
||||
if (lastExecutionInSecondsLimit != null) {
|
||||
await Future.delayed(const Duration(milliseconds: 2000));
|
||||
}
|
||||
} finally {
|
||||
await apiService.close(() {});
|
||||
stopwatch.stop();
|
||||
}
|
||||
|
||||
Log.info('eu.twonly.periodic_task finished after ${stopwatch.elapsed}.');
|
||||
return;
|
||||
Log.info('Periodic task finished after ${stopwatch.elapsed}.');
|
||||
return receiveMessage;
|
||||
}
|
||||
|
||||
Future<void> handleProcessingTask() async {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|||
|
||||
if (Platform.isAndroid) {
|
||||
if (isInitialized) {
|
||||
await handlePeriodicTask(lastExecutionInSecondsLimit: 3);
|
||||
await backgroundFetch(lastExecutionInSecondsLimit: 3);
|
||||
}
|
||||
} else {
|
||||
// make sure every thing run...
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import 'package:twonly/globals.dart';
|
|||
import 'package:twonly/locator.dart';
|
||||
import 'package:twonly/src/constants/secure_storage.keys.dart';
|
||||
import 'package:twonly/src/model/protobuf/client/generated/push_notification.pb.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/fcm.background.dart';
|
||||
import 'package:twonly/src/services/notifications/pushkeys.notifications.dart';
|
||||
|
|
@ -167,6 +168,15 @@ class FcmNotificationService {
|
|||
// This is just a workarround until the new Rust decryption is enrolled fully.
|
||||
final pushDataString = message.data['push_data'] as String?;
|
||||
if (pushDataString != null) {
|
||||
if (apiService.isConnected) {
|
||||
Log.info('Got FCM message, but API is connected...');
|
||||
} else {
|
||||
Log.info('Trying to connect to the API in the background.');
|
||||
|
||||
if (await backgroundFetch()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final pushDataBytes = base64Decode(pushDataString);
|
||||
final encryptedPush = EncryptedPushNotification.fromBuffer(
|
||||
|
|
@ -214,6 +224,7 @@ class FcmNotificationService {
|
|||
Log.error('Error handling push_data: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (message.notification != null || message.data['title'] != null) {
|
||||
final title =
|
||||
|
|
|
|||
Loading…
Reference in a new issue