firebase - 在 Flutter 上单击背景通知时如何导航?

标签 firebase flutter dart background firebase-cloud-messaging

单击 时是否可以导航到指定路径背景 FCM 通知?
我创建了一个 顶级功能并将其添加到导航器路径但它不起作用,当单击后台通知时,它只是打开应用程序
我想我发现了一个问题

Now, I changed fcm configuration from home page to splash screen. The foreground doesn't navigate to the page, I think its because the Splash Screen is no longer available. When I click on the notification message, it just opens the app.


FCM 配置
onBackgroundMessage: backgroundMessageHandler
顶级功能
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) {
  if (message.containsKey('data')) {
    getIt<NavigationService>().navigateTo('/${message['data']['screen']}');
  }
}
有效载荷
const payload: admin.messaging.MessagingPayload = {
                notification:{
                    title: `New Enquiry`,
                    body:`${customerName} published to ${subName}`,
                    badge: '1',
                    sound: 'default'
                },
                data: {
                    click_action: `FLUTTER_NOTIFICATION_CLICK`,
                    sound: `default`,
                    status: `chat`,
                    screen: `homePage`
                  }
            }
main.dart
GetIt getIt = GetIt.instance;

void main() {
  setupLocator();
    runApp(MyApp());
}
MaterialApp
 return MaterialApp(
      navigatorKey: NavigationService().navigatorKey,
      onGenerateRoute: Router.generateRoute,
    );
导航服务设置定位器
class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
      new GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState.pushNamed(routeName);
  }
}

void setupLocator() {
  getIt.registerLazySingleton(() => NavigationService());
}

最佳答案

要处理来自后台状态的通知,您可以使用 FirebaseMessaging 库公开的流。

FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
  // Handle navigation or perform any logic here
});

关于firebase - 在 Flutter 上单击背景通知时如何导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589234/

相关文章:

angular - 为什么 .map 运算符在没有 .pipe 的情况下无法工作(Angular + Angularfire2/auth)?

android - 如何在 Firebase 中检索对象内的嵌套列表对象

flutter - Flutter 的自定义 CircularProgressIndicator

Flutter 没有将新用户插入到 Firebase

flutter - 转换后的JSON在listView中不显示任何内容

java - 无法将 java.lang.String 类型的值转换为 int

android - Firebase、Android、Google Plus 登录 - 无法检索用户的电子邮件地址

flutter - 无法从 Cloud Firestore 存档处于初始状态的单个数据

dart - 函数调用前的感叹号是什么意思?

dart - curry 函数的类型签名