首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振GoRouter与接收火基推送通知

颤振GoRouter与接收火基推送通知
EN

Stack Overflow用户
提问于 2022-06-24 14:35:19
回答 1查看 345关注 0票数 0

我不知道如何正确地集成Firebase消息传递(推送通知)和GoRouter。每个通知都可以具有"link“属性,以启用”深度链接“,例如:

代码语言:javascript
复制
 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    }

http://myapp/tasks/1深入链接到应用程序的地方,应用程序应该使用id 1打开“任务”的详细信息。

  • App通过adb或FCM "works“启用了深度链接
  • 链接,即GoRouter识别它并打开所需的路由

但是,通常情况下,通知包含一些“数据”--例如,UserId,它属于--我们的应用程序支持多个用户帐户。

代码语言:javascript
复制
 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    },
    "data": {
        "userId": "xy123"
    }

  • 如果当前登录的用户与我们希望第一次登录正确的通知不相同.

从理论上讲,这可以在redirect公司的GoRouter公司进行。

代码语言:javascript
复制
 GoRouter(
      observers: [_routeObserver],
      initialLocation: '/',
      routes: [
        ... routes
      ],
      refreshListenable: GoRouterRefreshStream(_loginState.stream),
      redirect: (state) {
        // here we can determine if user is logged in, redirect to sign in page, etc...
        return null;
      });

从FCM方面我们可以收听即将到来的消息

代码语言:javascript
复制
 FirebaseMessaging.onMessageOpenedApp.listen((msg) {
      final data = msg.data;
      final userId = data['userId'];
      print('received message opened');
    });

从我的测试

调用terminated)

  • Then,

  • Firstly onMessageOpenedApp (如果应用程序处于后台,但不调用
  1. 的重定向方法,而路由器被重定向到通知的链接

)

如何提取通知的附加数据并将其传递到GoRouter并在重定向方法中使用它

在一个较旧的应用程序中,应用程序监听接收到的消息,解析接收到的消息,处理它们的数据,然后手动地重定向到正确的页面。,但我希望使用GoRouter和DeepLinks特性。

EN

回答 1

Stack Overflow用户

发布于 2022-09-04 02:14:12

首先,我只是想知道为什么不将通知发送给所有者( If currently logged user is not same as from notification we want first login correct one. )。但是,如果需要的话,您可以添加如下条件。

因此,go_router允许我们从路由器声明下面的上下文导航或重定向。因此,refreshListenable不仅仅是针对loginState的,我更像是appRouterState。因此,在appRouterState内部,将结束所有影响应用程序内部导航的状态,如isLoggedIn comingPath,然后可以在重定向中添加逻辑。

代码语言:javascript
复制
class AppRouterState {
    final String? comingPath; // link that you extract from notification payload
    final String? email; // if email is null isNotLoggedIn
    ...
}

那么你的GoRouter就像

代码语言:javascript
复制
GoRouter(
    debugLogDiagnostics: true,
    refreshListenable: GoRouterRefreshStream(appRouterStateNotifier.stream),
    redirect: (GoRouterState state) {
      String? redirection(GoRouterState state) {
        final appRouterState = ref.read(appRouterStateNotifierProvider);
        final isAuthed = appRouterState.email != null;

        if (appRouterState.comingPath != state.location && appRouterState.comingPath != null) {
            return appRouterState.comingPath;
        }

        if (state.location != '/login' && !isAuthed) return '/login';
        if (state.location == '/login' && isAuthed) return '/';

        return null;
      }
      final result = redirection(state);
      return result;
    },
)

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72745661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档