我使用 flutter 本地通知为传入的 FCM 创建了一个处理程序,点击每个通知它可以导航到特定屏幕,并且只要应用程序位于前台和后台,该处理程序就可以正常工作,但是当应用程序终止时,它会将我导航到而是主屏幕。
在后台情况下我使用 FirebaseMessaging.onBackgroundMessage() 作为侦听器并终止 & FirebaseMessaging.onMessage.listen() 在后台情况下使用 flutterLocalNotificationsPlugin.show() 在这两种情况下
当应用程序关闭时,您需要检查打开应用程序时单击的通知。
用途:
final RemoteMessage? initialMessage =
await FirebaseMessaging.instance.getInitialMessage();
在初始化应用程序时在某处获取初始消息并在其不为空时进行处理
有时,您可能需要在应用程序的其余部分初始化后添加短暂的延迟来处理它。你可以用
Future.delayed(const Duration(milliseconds: 300)).then((value){
// handle it in here
)};
致电后别忘了这样做
await Firebase.initializeApp();
您可以用于在应用程序终止时打开本地通知。
final notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) {
final message = notificationAppLaunchDetails !.notificationResponse;
}