【发布时间】:2020-01-12 05:58:54
【问题描述】:
我希望我的 Flutter 应用在单击本地通知时打开一个页面。我在事件监听器中定义了以下代码:
Navigator.push(
Ccontext,
MaterialPageRoute(builder: (context) => PendingOrders()),
);
debugPrint('notification payload: ' + payload);
事件监听器执行成功,并在调用本地通知时打印 debugPrint 的参数,但无法打开 PendingOrders 路由。
这是 main.dart 中的完整代码
class _MyAppState extends State<MyApp> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
var android = AndroidInitializationSettings('mipmap/ic_launcher');
var ios = IOSInitializationSettings();
var platform = InitializationSettings(android, ios);
flutterLocalNotificationsPlugin.initialize(platform,onSelectNotification: onSelectNotification);
_firebaseMessaging.subscribeToTopic('order-created');
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
showNotification(message);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
print("Hafijur");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: ${message['notification']['data']['click_action']}");
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
}
showNotification(Map<String, dynamic> msg) async {
var android = new AndroidNotificationDetails(
'sdffds dsffds',
"CHANNLE NAME",
"channelDescription",
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin.show(
0, "New order arrived", "Hey, hurry up! you have a order to deliver", platform,payload: "order created");
}
Future onSelectNotification(String payload) async {
this.build(context);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PendingOrders()),
);
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: Constants.lightTheme,
debugShowCheckedModeBanner: false,
routes: {
"/": (context) => MainScreen(),
"/login": (context) => LoginPage(),
"/dashboard": (context) => Dashboard(),
"/all_orders": (context) => AllOrders(),
"/pending_orders": (context) => PendingOrders(),
"/delivered_orders": (context) => DeliveredOrders(),
"/order": (context) => Order(),
},
);
}
}
【问题讨论】:
-
面临同样的问题
-
onSelectNotification 不工作如果应用程序关闭任何希望??