Initialize app on bg push

This commit is contained in:
vishnukvmd 2021-10-13 10:54:49 +05:30
parent cd0ecf32bd
commit bbad8a4cce
2 changed files with 22 additions and 17 deletions

View file

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:background_fetch/background_fetch.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
@ -151,6 +152,7 @@ Future<void> _init(bool isBackground) async {
await MemoriesService.instance.init();
await LocalSettings.instance.init();
await PushService.instance.init();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FeatureFlagService.instance.init();
_logger.info("Initialization done");
_initializationStatus.complete();
@ -292,3 +294,17 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
});
}
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (_initializationStatus == null) {
// App is dead
_runWithLogs(() async {
_logger.info("Background push received");
await _init(true);
UpdateService.instance.showUpdateNotification();
await _sync(isAppInBackground: true);
}, prefix: "[bg]");
} else {
// App has already been initialized, will let foreground handle everything
}
}

View file

@ -28,26 +28,15 @@ class PushService {
_logger.info("token " + await FirebaseMessaging.instance.getToken());
_logger
.info("APNS token " + await FirebaseMessaging.instance.getAPNSToken());
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
_logger.info('init complete');
}
}
final _logger = Logger("PushService");
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
_logger.info("Handling a background message: ${message.messageId}");
_handlePushMessage(message);
}
void _handlePushMessage(RemoteMessage message) {
_logger.info('Message data: ${message.data}');
if (message.notification != null) {
_logger
.info('Message also contained a notification: ${message.notification}');
}
if (message.data != null && message.data["purpose"] == "sync") {
void _handlePushMessage(RemoteMessage message) {
_logger.info('Message data: ${message.data}');
if (message.notification != null) {
_logger.info(
'Message also contained a notification: ${message.notification}');
}
SyncService.instance.sync();
}
}