无法在 onMessage FCM 中触发 alterdialog
Cannot fire alterdialog inside onMessage FCM
我的想法是,当应用程序处于前台时,我想显示一个警告对话框,当用户按下对话框中的确定按钮时,它会将他们导航到另一个屏幕。但是,不会出现 alterdialog。如果我删除 alertdialog 并只保留导航代码,那么应用程序会在通知到达后立即导航到另一个屏幕。这是一种糟糕的用户体验,因为用户无法控制 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;
},
);
我的想法是,当应用程序处于前台时,我想显示一个警告对话框,当用户按下对话框中的确定按钮时,它会将他们导航到另一个屏幕。但是,不会出现 alterdialog。如果我删除 alertdialog 并只保留导航代码,那么应用程序会在通知到达后立即导航到另一个屏幕。这是一种糟糕的用户体验,因为用户无法控制 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;
},
);