点击带有颤振的FCM通知时如何移动到另一个屏幕?



我在我的flutter应用程序上创建了一个通过FCM接收通知的方法。现在,在单击此通知后,我想移动到另一个屏幕。这是代码:

Future<List<String>> registerForegroundMessageHandler() async {
Completer<List<String>> completer = Completer<List<String>>();
FirebaseMessaging.onMessage.listen((remoteMessage) {
AppLogger.log(" --- foreground message received ---", "");
AppLogger.log("Title", remoteMessage.notification!.title);
AppLogger.log("Body", remoteMessage.notification!.body);
String smallIcon = 'drawable/small_launcher_icon';
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('channel_id', 'channel_name',
importance: Importance.high,
priority: Priority.high,
ticker: 'New message received',
icon: smallIcon,
playSound: true,
enableVibration: true,
styleInformation: const DefaultStyleInformation(true, true),
//sound: RawResourceAndroidNotificationSound('notification'),
largeIcon:
const DrawableResourceAndroidBitmap('mipmap/launcher_icon'),
color: const Color.fromARGB(255, 0, 123, 255),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ongoing: true,
ledOffMs: 500);
NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
// Display the notification
flutterLocalNotificationsPlugin.show(
0, // Notification ID
remoteMessage.notification?.title ?? 'default title',
remoteMessage.notification?.body ?? 'default body',
notificationDetails,
);
final version = remoteMessage.data['version'];
final size = remoteMessage.data['size'];
AppLogger.log("Version", version);
if (version != null) {
AppLogger.log("Version:", version);
downloadFile.messageHandler(remoteMessage);
} else {
AppLogger.log("No version found in message data.", "No");
}
completer.complete(['$size', 'Version $version']);
});
AppLogger.log("Version2, Size", await completer.future);
return await completer.future;
}

为了得到预期的结果,需要进行哪些更新?

您可以使用onMessageOpenedApp在点击推送通知时重定向。

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('ffffffffffffffffffff');
print("onMessageOpenedApp: $message");
Navigator.pushReplacement(NavigationService.navigatorKey.currentContext!, SlideLeftRoute(page: NotificationScreenView()));
});

要从Kill状态重定向应用程序,您可以使用getInitialMessage功能重定向到特定的屏幕

FirebaseMessaging.instance.getInitialMessage().then((message) {
if (Platform.isAndroid){
if (message != null) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
//  TODO
//  Do any logic, for example dialog
Navigator.pushReplacement(NavigationService.navigatorKey.currentContext!, SlideLeftRoute(page: NotificationScreenView()));
}
}
}else{
if (message != null) {
appConstants.fromBacground.value = true;
Navigator.pushReplacement(NavigationService.navigatorKey.currentContext!, SlideLeftRoute(page: NotificationScreenView()));
}
}

});