当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)

Posted

技术标签:

【中文标题】当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)【英文标题】:unable to navigate to particular screen on push notification click when app is killed(Flutter android) 【发布时间】:2021-08-02 14:03:10 【问题描述】:

我在我的颤振应用程序中使用 Firebase 云消息传递。单击推送通知时,我无法导航到特定屏幕。 当应用程序在前台并且应用程序在后台时,它可以正常工作,但在应用程序被杀死时就不行了。

代码:

class FcmHelper 
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  new FlutterLocalNotificationsPlugin();
  void initializeFcm() 
    var initializationSettingsandroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher');


    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message)       
        displayNotification(message);
        return;
      ,
      onResume: (Map<String, dynamic> message) 
        displayNotification(message);
        return;
      ,
      onLaunch: (Map<String, dynamic> message) 
        displayNotification(message);
        return;
      ,

    );
 
  


  Future displayNotification(Map<String, dynamic> message) async 
    // display notification
  

  Future onSelectNotification(String payload) async 
    try 
      await flutterLocalNotificationsPlugin.cancelAll();
      var tripDetails = HotelBookingDetails(
          );
      Navigator.pushAndRemoveUntil(
          GlobalVariable.navState.currentContext,
          MaterialPageRoute(builder: (context) => tripDetails),
          ModalRoute.withName('/'));
     catch (e, s) 
      debugPrint(e.toString());
    
  


【问题讨论】:

请在此处粘贴相关代码 【参考方案1】:
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) 
    // add your condition if any
    Navigator.of(context).pushNamedAndRemoveUntil("/notifications", (route) => route.settings.name == "/notifications" ? false : true);
);

【讨论】:

【参考方案2】:
final noti = FirebaseMessaging.instance.getInitialMessage();

您在初始屏幕中实现此功能并转到您想要的屏幕

【讨论】:

【参考方案3】:

onLaunch 回调将在 FirebaseMessaging 初始化后触发, 也许你可以像这样使用navigatorKey 导航

      GlobalVariable.navState.currentState.push(
                        MaterialPageRoute(
                          builder: (_) => YourPage(),
                        ),
                      );

如果在上下文可用时尝试初始化 FirebaseMessaging 没有帮助,可能在 MateriApp 构建器方法中

MaterialApp(
  builder: (context, widget) 
     FcmHelper().initializeFcm();
   
);

【讨论】:

以上是关于当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController

如何在 ios 中单击通知时接收数据,当应用程序被杀死时,(react-native-fcm 模块在 IOS 中获取推送通知)

当应用程序被杀死时,在推送通知后执行操作:react-native,IOS

当应用程序被杀死Android时无法接收解析推送通知

在应用程序未运行时处理推送通知(应用程序被杀死)

当应用程序不在前台时,特定活动不会从通知单击打开(应用程序是最近的,没有被杀死!)