我已将 Firebase 消息传递更新为最新版本,但现在我无法掌握如何实施新修改。
到目前为止我已经使用过:
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
和
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
这是我的原始代码
class PushNotificationService{
final FirebaseMessaging fcm = FirebaseMessaging();
Future initialize(context) async {
if(Platform.isIOS){
fcm.requestNotificationPermissions(IosNotificationSettings());
}
fcm.configure(
onMessage: (Map<String, dynamic> message) async {
fetchRideInfo(getRideID(message), context);
},
onLaunch: (Map<String, dynamic> message) async {
fetchRideInfo(getRideID(message), context);
},
onResume: (Map<String, dynamic> message) async {
fetchRideInfo(getRideID(message), context);
},
);
}
似乎无法找到实现它的方法。 有人可以帮忙吗?
答案 0 :(得分:0)
您应该执行以下操作:
initialize
方法的主体以包含 FirebaseMessaging.onMessage
和 FirebaseMessaging.onMessageOpenedApp
侦听器FirebaseMessaging.instance.getInitialMessage()
获取初始消息并在消息不为空时运行 onLaunch
回调(即应用在终止时从通知启动) Future initialize(context) async {
if(Platform.isIOS){
fcm.requestNotificationPermissions(IosNotificationSettings());
}
//onMessage
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
fetchRideInfo(getRideID(message), context);
});
//onResume
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
fetchRideInfo(getRideID(message), context);
});
//onLaunch
final Remote initialMessage = await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
fetchRideInfo(getRideID(initialMessage), context);
}
}