颤抖的gorouter和接收壁炉推动通知
我无法弄清楚如何正确整合火箱消息传递(推送通知)和gorouter。每个通知都可以具有“链接”属性来启用“深链接”,例如:
"notification": {
"body" : "First Notification",
"title": "App Testing",
"link": "http://myapp/tasks/1"
}
其中http:// myapp/tasks/1
是深层链接到应用程序 - 应用程序应打开的详细信息。 ID 1。的“任务”。
- 应用程序已启用了Deep-links( https://docs.flutter.flutter.dev/dev/dev/development /ui/avarigation/deep -linking )
- 通过ADB或通过FCM“ Works”进行深链接 - 也就是说,Gorouter识别它并打开所需的路由,
但是通常的通知包含一些“数据” - 例如用户,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');
});
来自我的测试
- ,首先是
onMessageOpenedApp
被调用(如果应用程序在后台,但是未终止) - ,然后将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..
- App has enabled deep-links (https://docs.flutter.dev/development/ui/navigation/deep-linking)
- Deep links via adb or via FCM "works" - that is, GoRouter recognize it and open desired route
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
- Firstly
onMessageOpenedApp
is called (in case app was in background, but not terminated) - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我只是想知道为什么不将通知发送给所有者(
如果当前记录的用户与从通知中的用户不同,我们希望先登录正确的一个。
)。但是,如果需要,您可以添加下面的条件。因此,go_router允许我们从路由器声明以下的上下文或重定向的情况下导航。因此,
刷新可恢复
不仅适用于登录,而且我会喜欢affouterstate
。因此,在内部,AxpoutouterState
将结束所有状态,这些状态将影响您的应用程序内的导航,例如iSloggedin
codath
codath ,您可以在重定向内添加逻辑然后,您的Gorouter会像
希望这会有所帮助。
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 likeappRouterState
instead. So inside theappRouterState
would wrap up all the state that will effect the navigation inside your app likeisLoggedIn
comingPath
then you can add logics inside the redirectThen your GoRouter would be like
Hope this helps.