简体   繁体   English

为什么在颤动中点击通知时路由不起作用? (项目基于GetX和flutter本地推送通知包)

[英]why routing is not working when tap on the notification in flutter? ( the project is based on GetX and flutter local push notification package )

Good day.再会。 I know that we use the onSelectNotification property to get the callback when the user tap on the notification in using the flutter local push notification package.我知道当用户在使用 flutter 本地推送通知包时点击通知时,我们使用 onSelectNotification 属性来获取回调。

  Future<void> _initialize() async {
    await _configureLocalTimeZone();
    AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings(
            onDidReceiveLocalNotification: _onDidReceiveLocalNotification);

    InitializationSettings initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: _onSelectNotification);        // This is for the callback
  }

This is the _onSelectsNotification function.这是 _onSelectsNotification 函数。

  Future _onSelectNotification(String payload) async {
    if (payload != null) {
      print('notification payload: $payload');
    }
    BuildContext context;
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
      return NotificationsView();
    }));                           // ***** This is not working. with no error.
  }

I am not sure what to use for the context.我不确定在上下文中使用什么。 I think the next route to the NotificationView() is not working because of the context value.我认为由于上下文值,到 NotificationView() 的下一个路由不起作用。 Could you please help me with this issue?你能帮我解决这个问题吗? I am now using the GetX for the state management.我现在使用 GetX 进行状态管理。

Thanks.谢谢。

I solved this problem with Get.toNamed() My fault was in not returning in the _onSelectNotification function.我用 Get.toNamed() 解决了这个问题我的错是在 _onSelectNotification 函数中没有返回。

  Future _onSelectNotification(String payload) async {
    if (payload != null) {
      print('notification payload: $payload');
    }
    return Get.toNamed(Routes.NOTIFICATIONS);
  }

This is working well.这运行良好。

But I still don't know how to get the context from another state class.但是我仍然不知道如何从另一个状态类中获取上下文。 In the previous post, I passed the null for the context.在上一篇文章中,我为上下文传递了 null。 I know that this was wrong.我知道这是错误的。 How to get?怎么获得?

The code is not working because by the time you call your Navigator, the widget is not finished building.A simple fix is to wrap the Navigator.of(context) with Future like this.代码不起作用,因为当您调用 Navigator 时,小部件还没有完成构建。一个简单的解决方法是像这样用 Future 包装 Navigator.of(context)。

Future.delayed(Duration.zero).then((_){
Navigator.of(context).push(
    MaterialPageRoute(builder: (BuildContext context) {
      return NotificationsView();
    }
  ));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter IOS 推送通知不起作用 - Flutter IOS push notification not working Flutter firebase推送通知不会路由到特定页面 - Flutter firebase push notification not routing to specific page flutter_local_notification 分组通知不起作用 - flutter_local_notification Grouping notification not working Flutter 本地通知声音不工作 - Flutter Local Notification Sound not working Flutter:本地通知插件,当用户点击通知时导航到特定屏幕 - Flutter: local notification plugin, navigate to specific screen when the user tap the notification Flutter 本地通知 package 在安排不在几个小时范围内的通知时不起作用 - Flutter local notifications package not working when scheduling a notification not in the range of a few hours 在 Flutter 中,Getx 包 unknownRoute 不起作用 - In Flutter, Getx package unknownRoute not working 应用程序处于前台时,Flutter FCM推送通知不起作用 - Flutter FCM Push Notification Not Working when app is in foreground Flutter 推送通知仅在应用程序处于后台时有效 - Flutter push notification is working only when app is in background 如何使用 flutter 本地通知推送通知? 我的不工作 - How to push notification using flutter local notification? mine does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM