我已使用 flutter_local_notifications ,推送通知已成功接收,当我单击它时,我试图重定向到特定屏幕,但它始终重定向到主屏幕。
下面是我的代码。
@override
void initState() {
// TODO: implement initState
super.initState();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
var data = message['data'] as Map;
var msg1 = data['message'] as String;
var response = json.decode(msg1) as Map;
String str_title = response['title'] as String;
String str_body = response['body'] as String;
_showNotification(str_title, str_body, msg1);
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
}
_showNotification功能代码
void _showNotification(String title, String body, String pay_load) async {
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
AppConstant.notify_channel_id,
AppConstant.notify_channel_name,
AppConstant.notify_channel_desc,
importance: Importance.Max,
priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: pay_load);
}
onSelectNotification功能代码
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
// here set and put condition for property id
var response = json.decode(payload) as Map;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
PropertyDetailScreen()),
);
PropertyDetailScreen 是我在单击通知时要重定向的屏幕,但是它始终重定向到主屏幕,因此请指导我哪里我出错了,或者为什么我的代码不起作用。
答案 0 :(得分:2)
按以下说明使用GlobalKey尝试
在内部声明
class _MyAppState extends State<MyApp> {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "MainNavigator");
}
在MaterialApp中分配
MaterialApp(
navigatorKey: navigatorKey,
supportedLocales: [
Locale('en', 'US'),
Locale('ar', ''),
Locale('it'),
Locale('fr'),
Locale('es'),
Locale('de'),
Locale('pt'),
],
);
用作navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => YourClassName()));