【问题标题】:onSelectNotification Navigator not working if app closed flutter如果应用程序关闭颤动,onSelectNotification Navigator 将不起作用
【发布时间】:2020-08-02 02:52:23
【问题描述】:

应用打开时的onSelectNotification

  Future<void> onSelectNotification(String payload) async {
    Category category = Category();
    category.id = notification.idNew;
    category.email = notification.mainphoto;
    category.since = notification.since;
    category.name = notification.title;
    await Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => FourthRoute(
                  category: category,
                )));
  }

当打开并点击通知时,它会转到FourthRoute

但是当它关​​闭时它只是打开应用程序导航器不工作

ios 和 android 的问题

我想在 onSelectNotification 中使用 SharedPreferences 来保存密钥 稍后再检查

但我读到应用程序关闭时飞镖不工作

【问题讨论】:

    标签: flutter notifications localnotification


    【解决方案1】:
    var details = await NotificationService()
        .flutterLocalNotificationsPlugin
        .getNotificationAppLaunchDetails();
    if (details.didNotificationLaunchApp) {
        print(details.payload);
    }
    

    在应用入口中使用此代码,它可以获得通知点击的有效负载

    【讨论】:

      【解决方案2】:

      Navigator 必须作为子组件包装在 MaterialApp 小部件中,否则,您必须使用 navKey 来引用您的 materialApp。

      1. final navKey = new GlobalKey<NavigatorState>();
      
      2. MaterialAPP(title: 'title', ..., navigatorKey: navKey)
      
      3. And then use the key to route to your widget, 
      navKey.currentState.push(MaterialPageRoute(builder: (context) => NewRoute()));
      

      【讨论】:

        【解决方案3】:

        我联系插件支持

        Here

        Future<void> main() async {
          // needed if you intend to initialize in the `main` function
          WidgetsFlutterBinding.ensureInitialized();
        
          notificationAppLaunchDetails =
              await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
        
          var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
          // Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
          // of the `IOSFlutterLocalNotificationsPlugin` class
          var initializationSettingsIOS = IOSInitializationSettings(
              requestAlertPermission: false,
              requestBadgePermission: false,
              requestSoundPermission: false,
              onDidReceiveLocalNotification:
                  (int id, String title, String body, String payload) async {
                didReceiveLocalNotificationSubject.add(ReceivedNotification(
                    id: id, title: title, body: body, payload: payload));
              });
          var initializationSettings = InitializationSettings(
              initializationSettingsAndroid, initializationSettingsIOS);
          await flutterLocalNotificationsPlugin.initialize(initializationSettings,
              onSelectNotification: (String payload) async {
            if (payload != null) {
              debugPrint('notification payload: ' + payload);
            }
            selectNotificationSubject.add(payload);
          });
          runApp(
            MaterialApp(
              home: HomePage(),
            ),
          );
        }
        

        【讨论】:

        • 这很困惑,onSelectNotification vs selectNotificationSubject?你能提供更具体的细节吗?
        猜你喜欢
        • 2021-06-24
        • 2022-01-21
        • 2021-08-19
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 2018-08-09
        • 2017-06-23
        • 2019-11-25
        相关资源
        最近更新 更多