The outline of what I am trying to achieve is the following:
- Within the main Flutter app, the user authenticates with FirebaseAuth and afterwards can perform FirebaseFirestore read/writes.
- The app can launch a notification via flutter_local_notifications plugin. These notifications have actions that should also perform FirebaseFirestore read/writes.
The rough implementation is that my main() function does something like this:
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Later on ...
DocumentSnapshot myQuery = await FirebaseFirestore.instance.collection('myCollection').get();
The notification implementation works somewhat like this:
@pragma('vm:entry-point')
void onDidReceiveBackgroundNotificationResponse(NotificationResponse notificationResponse) async {
// This condition seems to be always true
if (Firebase.apps.isEmpty) {
await Firebase.initializeApp();
}
// This operation finishes successfully
await FirebaseFirestore.instance.collection('myCollection').doc('myDoc').set(notificationResponse.payload.toMap());
}
While the app on its own does everything as expected and the notification action also carries out successfully, they don't work in combination. After the notification action is carried out, queries from within the app won't work anymore, requesting me to call Firebase.initializeApp()
Apart from checking if Firebase was initialized before every query (which is not a viable solution because the app is also subscribed to FirebaseFirestore streams, which also stop working), is there a solution so that both the app and notification isolates can perform Firestore queries without blocking each other?
There seems to be surprisingly little information online on this topic, so I really hope you can help me out.