我在以 Firebase 为后端的颤振应用程序上收到两种通知,FLUTTER_NOTIFICATION_CLICK
云功能中实现以提供点击功能,
现在我想要的是,因为有两种不同的通知,我希望根据通知值导航特定屏幕。我应该怎么做?
以下是我正在使用的代码data payloads
和flutter code
有效载荷 1
const payload = {
notification : {
title: item title,
body: notificaionmessage,
icon:"default"
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
notification_id: notification_id,
//senderAvatarURL: messageRecieverSenderAvatar,
category: 'default'
}
};
有效载荷 2
const payload = {
notification: {
title: `NOTIFICATION2 `,
body: contentMessage,
badge: '1',
sound: 'default'
},
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
notification2: notificationid2,
//senderAvatarURL: messageRecieverSenderAvatar,
category: 'default'
}
}
用于处理通知的代码
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
print('onMessage: $message');
Platform.isAndroid ? showNotification(message['notification']) : showNotification(message['aps']['alert']);
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
return;
}, onResume: (Map<String, dynamic> message) {
print('onResume: $message');
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
return;
}, onLaunch: (Map<String, dynamic> message) {
print('onLaunch: $message');
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
return;
});
如上所述,导航已配置,但我希望它根据有效载荷进入不同的屏幕,例如PAYLOAD 1 to SCREEN1
和PAYLOAD 2 to SCREEN 2
我应该如何处理?
您可以通过多种方式做到这一点。
例如,您可以检查
click_action:"Move_To_Screen1">
if (message['data']['click_action']=='Move_To_Screen1')
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN1()));
else if (message['data']['click_action']=='Move_To_Screen2')
Navigator.push(context, MaterialPageRoute(builder: (context)=> SCREEN2()));
希望有帮助.