无法在onMessage FCM内激发alterdialog



这个想法是,当应用程序在前台时,我想显示一个警报对话框,当用户按下对话框中的OK按钮时,它会将它们导航到另一个屏幕。但是,alterdialog不会出现。如果我删除警报对话框,只保留导航代码,那么通知一到,应用程序就会导航到另一个屏幕。这是一种糟糕的用户体验,因为用户无法控制UI。我希望用户确认他们想要导航到不同的屏幕。因此使用了alertdialog。

这是我的代码:

void initState(){ 
super.initState();
var initializationSettingsAndroid  = AndroidInitializationSettings('@drawable/splash');
var initializationSettings = InitializationSettings(android:initializationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(initializationSettings);

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Inside onmessage'); 
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: '@drawable/splash',
playSound: true
),
));

AlertDialog(
title:Text(notification.title),
actions:  <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () {
setState(() {
//Navigator.of(context).pop();
navigatorKey.currentState.push(
MaterialPageRoute(builder: (_) => OrdersScreen()));
});
}),
],
content:SingleChildScrollView(
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:[
Text(notification.body)
]
)
)
);
}
});
}

您的代码只表达AlertDialog,但它对它没有任何作用。这就是为什么它没有显示的原因。

解决方案:

您需要调用showDialog并将AlertDialog对象传递给它,如下所示:

final AlertDialog alertDialog = AlertDialog(
title: Text(notification.title),
actions: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () {
setState(() {
//Navigator.of(context).pop();
navigatorKey.currentState
.push(MaterialPageRoute(builder: (_) => OrdersScreen()));
});
}),
],
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body)])));
showDialog(
context: context// (or `navigatorKey.currentContext` in this case),
builder: (BuildContext context) {
return alertDialog;
},
);    

相关内容

  • 没有找到相关文章

最新更新