颤抖的gorouter和接收壁炉推动通知

发布于 2025-02-10 03:21:26 字数 2121 浏览 2 评论 0原文

我无法弄清楚如何正确整合火箱消息传递(推送通知)和gorouter。每个通知都可以具有“链接”属性来启用“深链接”,例如:

 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    }

其中http:// myapp/tasks/1 是深层链接到应用程序 - 应用程序应打开的详细信息。 ID 1。的“任务”。

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

 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    },
    "data": {
        "userId": "xy123"
    }
  • 如果当前记录的用户与通知中的用户不同,我们希望先登录正确的用户。

从理论上讲,它可以在redirect gorouter secion of fcm side中完成

 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;
      });

,我们可以收听即将到来的消息,IE

 FirebaseMessaging.onMessageOpenedApp.listen((msg) {
      final data = msg.data;
      final userId = data['userId'];
      print('received message opened');
    });

来自我的测试

  1. ,首先是onMessageOpenedApp被调用(如果应用程序在后台,但是未终止)
  2. ,然后将Gorouter的重定向方法调用,并且路由器被重定向到Notification的链接

我如何提取通知的附加数据并将其传递到Gorouter中并将其传递到Redirect方法中?

在一个较旧的应用程序中,应用程序正在聆听收到的消息,解析收到的消息,处理其数据,然后手动将其重定向到适当的页面。 ,但我希望使用Gorouter和Deeplinks功能。

I can't figure out how to properly integrate Firebase Messaging (Push notifications) and GoRouter. Each notification can have "link" property to enable "deep linking", for example:

 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    }

Where http://myapp/tasks/1 is deep link into the app - Application should open detail of "task" with id 1..

However typically a notification contains some "data" - for example UserId, whos notification belongs to - our application support multiple user accounts.

 "notification": {
        "body" : "First Notification",
        "title": "App Testing",
        "link": "http://myapp/tasks/1"
    },
    "data": {
        "userId": "xy123"
    }
  • If currently logged user is not same as from notification we want first login correct one.

Theoretically it could be done within redirect secion of GoRouter

 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;
      });

From FCM side we can listen for upcoming message, ie

 FirebaseMessaging.onMessageOpenedApp.listen((msg) {
      final data = msg.data;
      final userId = data['userId'];
      print('received message opened');
    });

From my testing

  1. Firstly onMessageOpenedApp is called (in case app was in background, but not terminated)
  2. Then, GoRouter's redirect method is called and router is redirected to notification's link

How can I extract notification's additional data and pass it into GoRouter and work with it within redirect method?

In an older app, app was listening for received messages, parsed received messages, processed their data, and then, manually, redirected to the proper page. But I would like to use GoRouter and DeepLinks feature.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2025-02-17 03:21:26

首先,我只是想知道为什么不将通知发送给所有者(如果当前记录的用户与从通知中的用户不同,我们希望先登录正确的一个。)。但是,如果需要,您可以添加下面的条件。

因此,go_router允许我们从路由器声明以下的上下文或重定向的情况下导航。因此,刷新可恢复不仅适用于登录,而且我会喜欢affouterstate。因此,在内部,AxpoutouterState将结束所有状态,这些状态将影响您的应用程序内的导航,例如iSloggedin codath codath ,您可以在重定向内添加逻辑

class AppRouterState {
    final String? comingPath; // link that you extract from notification payload
    final String? email; // if email is null isNotLoggedIn
    ...
}

然后,您的Gorouter会像

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;
    },
)

希望这会有所帮助。

First i just wonder why would not you send the notification to the owner ( If currently logged user is not same as from notification we want first login correct one. ). But if that required you can add the condition like below.

Hence go_router allows us to either navigate from the context that is below the router declaration or with redirecting. So, the refreshListenable is not just for loginState but i would be like appRouterState instead. So inside the appRouterState would wrap up all the state that will effect the navigation inside your app like isLoggedIn comingPath then you can add logics inside the redirect

class AppRouterState {
    final String? comingPath; // link that you extract from notification payload
    final String? email; // if email is null isNotLoggedIn
    ...
}

Then your GoRouter would be like

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;
    },
)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文