Reputation: 1258
I am making a simple application which uses a WebView
widget as it's main widget. I want to be able to send a notification to all users from the firebase console. The application has no authentication.
My MessageHandler (defined in main.dart)
class MessageHandler extends StatefulWidget {
@override
_MessageHandlerState createState() => _MessageHandlerState();
}
class _MessageHandlerState extends State<MessageHandler>{
final FirebaseMessaging _fcm = FirebaseMessaging();
@override
void initState(){
super.initState();
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("here");
print("onMessage: $message");
final snackbar = SnackBar(
content: Text(message['notification']['title']),
);
},
);
Main
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Simple App",
home: WebViewer()
);
}
}
The WebViewer Widget works so I have not added it to the question. On the Firebase Console, the message appears to have been sent
but the alert does not appear on the Android Emulator, nor does the snackbar
.
WebView
class WebViewer extends StatelessWidget {
final Completer<WebViewController> _controller = Completer<WebViewController>();
//FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text("Event Viewer")),
),
body: WebView(
initialUrl: "https://google.com",
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController){
_controller.complete(webViewController);
},
)
);
Upvotes: 0
Views: 256
Reputation: 3361
In order to show the snackbar, you have to reference the specific scaffold instance that your app has built and tell it to show a snackbar. If the context is handy, you can say Scaffold.of(context).showSnackbar( snackbar ); Since you don't have access to the context, another way to reference the Scaffold is to define a key for the scaffold and then make sure you assign that key to the webviewer's scaffold.
You can say
class MessageHandler extends StatefulWidget {
// pass a reference to the scaffold key to the message handler when it is created
final GlobalKey<ScaffoldState> scaffoldKey;
MessageHandler({this.scaffoldKey});
@override
_MessageHandlerState createState() => _MessageHandlerState();
}
class _MessageHandlerState extends State<MessageHandler>{
final FirebaseMessaging _fcm = FirebaseMessaging();
@override
void initState(){
super.initState();
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("here");
print("onMessage: $message");
final snackbar = SnackBar(
content: Text(message['notification']['title']),
);
// reference the scaffold with its key and call showSnackBar
widget.scaffoldKey.currentState.showSnackbar(snackbar);
},
);
Then define the scaffold's key somewhere it can be passed to the message handler and the scaffold:
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
....
Scaffold(
key: scaffoldKey,
...
More info:
Upvotes: 1