我不知道如何正确地集成Firebase消息传递(推送通知)和GoRouter。每个通知都可以具有"link“属性,以启用”深度链接“,例如:
"notification": {
"body" : "First Notification",
"title": "App Testing",
"link": "http://myapp/tasks/1"
}在http://myapp/tasks/1深入链接到应用程序的地方,应用程序应该使用id 1打开“任务”的详细信息。
。
但是,通常情况下,通知包含一些“数据”--例如,UserId,它属于--我们的应用程序支持多个用户帐户。
"notification": {
"body" : "First Notification",
"title": "App Testing",
"link": "http://myapp/tasks/1"
},
"data": {
"userId": "xy123"
}从理论上讲,这可以在redirect公司的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;
});从FCM方面我们可以收听即将到来的消息
FirebaseMessaging.onMessageOpenedApp.listen((msg) {
final data = msg.data;
final userId = data['userId'];
print('received message opened');
});从我的测试
调用terminated)
onMessageOpenedApp (如果应用程序处于后台,但不调用)
如何提取通知的附加数据并将其传递到GoRouter并在重定向方法中使用它
在一个较旧的应用程序中,应用程序监听接收到的消息,解析接收到的消息,处理它们的数据,然后手动地重定向到正确的页面。,但我希望使用GoRouter和DeepLinks特性。
发布于 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,然后可以在重定向中添加逻辑。
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;
},
)希望这能有所帮助。
https://stackoverflow.com/questions/72745661
复制相似问题