'Flutter Background messages internationalization
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 ?
Solution 1:[1]
In MyApp create variable static Exp:
static BuildContext? mContext;
in build function of MyApp
mContext = context;
Solution 2:[2]
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
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cuong Truong Quoc |
| Solution 2 | ramkumar |
