Flutter Firebase消息传递-处理通知导航以进行前景和背景点击

问题描述

在我的一个应用中,希望基于id的通知处理,无论是前台还是后台。我无法管理。

我想要的是,当我收到推送通知时,应将用户导航到相应的屏幕。即使我们在当前任何屏幕上或应用处于后台。我之间也有启动画面。因此,即使我有一些东西要从Web api获取然后导航到其他屏幕,我也该如何管理导航。

无论应用程序处于前台还是后台模式,我都会收到推送通知。另外,按照我的以下代码接收通知时,不会显示任何吐司。

我添加了以下代码进行通知处理,

  final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");
  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  BuildContext _context;

  setupFirebaseMessaging() async {
    if (Platform.isIOS) {
      await _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true,badge: true,alert: true,provisional: false),);
    }

    _firebaseMessaging.configure(
      onMessage: (Map<String,dynamic> message) async {
        print("onMessage called : ${message.toString()}");
        notiMessage = message;
        getLocalNotification(message);
      },onLaunch: (Map<String,dynamic> message) async {
        showToast("onLaunch Called : ${navigatorKey.currentState}",context);
        print("onLaunch called : ${message.toString()}");
        navigateToDetailScreen(message);
      },onResume: (Map<String,dynamic> message) async {
        showToast("onResume Called : ${navigatorKey.currentState}",context);
        print("onResume called : ${message.toString()}");
        navigateToDetailScreen(message);
      },);
  }

  @override
  void initState() {
    super.initState();

    setupFirebaseMessaging();
  }

  @override
  Widget build(BuildContext context) {
    _context = context;
        return MaterialApp(
        navigatorKey: navigatorKey,debugShowCheckedModeBanner: false,title: 'Test',theme: ThemeData(
          primaryColor: primaryColor,accentColor: primaryDarkColor,// visualDensity: VisualDensity.adaptivePlatformDensity,),home: SplashScreen(),);

navigateToDetailScreen(Map<String,dynamic> message) async {
  if (message['data']['id'] ==1) {
  navigatorKey.currentState.pushAndRemoveUntil(MaterialPageRoute(builder: (context) => LoginScreen()),(Route<dynamic> route) => false);
  } else if (message['data']['id'] ==2) {
  // some async work
  navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => DetailScreen()));
  } else if (message['data']['id'] ==3) {
  // some async work
  navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => DataScreen()));
  } else {
  // some async work
  navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => HomeScreen()));
  } 
}

以上代码不适用于我的情况。我无法导航到所需的屏幕。另外,在使用异步计算处理通知导航时, SplashScreen 是您的初始屏幕如何生存?

有人能建议我这样做吗?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...