i've implemented a function to handle message received onbackground from FCM. Now i need to show something to user when the app is in background.
I've imported get_it injectable stacked_services
and tried to create a customdialog that show up without passing a context. Done when the app is open but nothing when app is in background.
in main Showdialog is initialized:
await setupLocator();
setupDialogUi();
PushNotificationsManager().init();
and a static dialogService too:
variables.dialogService = locator<DialogService>();
navigatorkey setted to
navigatorKey: StackedService.navigatorKey,
then, in FCM pushnotificationmanager i have:
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
print('on background $message');
variables.dialogService.showCustomDialog(
variant: DialogType.basic,
title: 'This is a custom UI with Text as main button',
description: 'Sheck out the builder in the dialog_ui_register.dart file',
mainButtonTitle: 'Ok',
);
}
class PushNotificationsManager {
PushNotificationsManager._();
factory PushNotificationsManager() => _instance;
static final PushNotificationsManager _instance = PushNotificationsManager._();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
bool _initialized = false;
Future<void> init() async {
if (!_initialized) {
// For iOS request permission first.
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
// showNotificationWithoutSound();
print('firebase ########test ########## on message $message');
variables.dialogService.showCustomDialog(
variant: DialogType.basic,
title: 'This is a custom UI with Text as main button',
description: 'Sheck out the builder in the dialog_ui_register.dart file',
mainButtonTitle: 'Ok',
);
// }
},
onBackgroundMessage: myBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print('firebase ########test ########## on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('firebase ########test ########## on launch $message');
},
);
// For testing purposes print the Firebase Messaging token
String token = await _firebaseMessaging.getToken();
print("FirebaseMessaging token: $token");
_initialized = true;
}
}
}
onMessage show correctly the dialog but myBackgroundMessageHandler throw an error:
NoSuchMethodError: The method 'showCustomDialog' was called on null.
Any suggestion to show a dialog or a widget or something when the app is in background?