In my flutter app, i want to translate some text in the push notification body.
For foreground notifications. there is no problem.
For backgroud notifiactions i use :
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
And _firebaseMessagingBackgroundHandler
must be a top-level function.
So, how can i use my famous AppLocalizations.of(context).cancel,
since I don't have a context here ?
CodePudding user response:
In MyApp create variable static Exp:
static BuildContext? mContext;
in build function of MyApp
mContext = context;
CodePudding user response:
Since _firebaseMessagingBackgroundHandler
is a top-level function- you can pass context
as an optional parameter. So that you can access the context.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message,
{BuildContext? context}){
//Your code
}