Blo*_*oss 13 background dart firebase flutter firebase-cloud-messaging
单击后台FCM 通知时是否可以导航到指定路径?
我创建了一个顶级函数并将其添加到导航器路径但它不起作用,当单击后台通知时,它只会打开应用程序
我想我发现了一个问题
现在,我将
fcm配置从更改home page为splash screen. 前台没有导航到页面,我认为这是因为启动画面不再可用。当我点击通知消息时,它只会打开应用程序。
FCM 配置
onBackgroundMessage: backgroundMessageHandler
Run Code Online (Sandbox Code Playgroud)
顶层函数
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
getIt<NavigationService>().navigateTo('/${message['data']['screen']}');
}
}
Run Code Online (Sandbox Code Playgroud)
有效载荷
const payload: admin.messaging.MessagingPayload = {
notification:{
title: `New Enquiry`,
body:`${customerName} published to ${subName}`,
badge: '1',
sound: 'default'
},
data: {
click_action: `FLUTTER_NOTIFICATION_CLICK`,
sound: `default`,
status: `chat`,
screen: `homePage`
}
}
Run Code Online (Sandbox Code Playgroud)
main.dart
GetIt getIt = GetIt.instance;
void main() {
setupLocator();
runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)
材料应用
return MaterialApp(
navigatorKey: NavigationService().navigatorKey,
onGenerateRoute: Router.generateRoute,
);
Run Code Online (Sandbox Code Playgroud)
NavigationService和setupLocator
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(String routeName) {
return navigatorKey.currentState.pushNamed(routeName);
}
}
void setupLocator() {
getIt.registerLazySingleton(() => NavigationService());
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此功能在应用程序被杀死时进行导航并在后台接收通知
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
print("FirebaseMessaging.getInitialMessage");
if (message != null) {
Navigator.of(context).pushNamed('/call');
}
});
Run Code Online (Sandbox Code Playgroud)
此函数仅在应用程序打开并获取最后一条消息时运行一次。
您可以在此文档中阅读更多内容:https://github.com/FirebaseExtended/flutterfire/blob/62e09975f9b3d14141585e62ddab6b98e667b7cf/docs/messaging/notifications.mdx
要处理来自后台状态的通知,您可以使用 FirebaseMessaging 库公开的流。
FirebaseMessaging.onMessageOpenedApp.listen((remoteMessage) {
// Handle navigation or perform any logic here
});
Run Code Online (Sandbox Code Playgroud)
在该示例中,我使用类 ID 等共享首选项数据来检查通知数据是否包含此类 ID,然后它可以导航到特定屏幕
void registerNotification() {
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true),
);
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
print('onMessage: $message');
// print(message['data']['classID']+" onMessage");
if(SharedClassID.toString().contains(message['data']['classID']))
{
Navigator.push(context, MaterialPageRoute(builder:
(context)=>BroadcastNotification()));
print("found");
}
else if(SharedClassID.toString().contains(message['data']['classID']) )
{
Navigator.push(context, MaterialPageRoute(builder: (context)=>MuseGalaryNew()));
print("found");
}
else
{
print("not found");
}
return;
}, onResume: (Map<String, dynamic> message) {
if(SharedClassID.toString().contains(message['data']['classID']))
{
Navigator.push(context, MaterialPageRoute(builder: (context)=>BroadcastNotification()));
print("found");
}
else if(SharedClassID.toString().contains(message['data']['classID']))
{
Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentHomework()));
print("found");
}
else
{
print("not found");
}
//print(message['data']['classID']+" onResume");
print('onResume: $message');
return;
}, onLaunch: (Map<String, dynamic> message) {
if(SharedClassID.toString().contains(message['data']['classID']) )
{
Navigator.push(context, MaterialPageRoute(builder: (context)=>BroadcastNotification()));
print("found");
}
else if(SharedClassID.toString().contains(message['data']['classID']))
{
Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentHomework()));
print("found");
}
else
{
print("not found");
}
return;
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7131 次 |
| 最近记录: |