如何在 Flutter 中使用最新版本配置 Firebase Messaging?
How to configure Firebase Messaging with latest version in Flutter?
我使用下面的代码行进行 flutter 通知配置的 firebase 消息配置,但现在在集成到最新版本的 firebase 消息后它给我错误
代码行
messaging.configure(onMessage: (Map<String, dynamic> message){}
DART 分析错误
error: The method 'configure' isn't defined for the type 'FirebaseMessaging'.
请检查以下示例。
@override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
}
FirebaseMessaging.configure() 被 firebase 团队删除:
原因:
如果多次调用 configure()
的先前实现会导致意外的副作用(注册不同的处理程序或删除处理程序)。此更改允许开发人员更明确地注册和删除处理程序,而不会通过 Streams 影响其他人。
使用FirebaseMessaging.onMessage
方法获取消息
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
使用 FirebaseMessaging.onMessageOpenedApp
替代 onLaunch
和 onResume
处理程序。
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
基于 Jitesh 的回答,对我来说,需要实施 getInitialMessage
才能在应用程序终止时使导航正常工作(替换 onLaunch)
// workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('getInitialMessage data: ${message.data}');
_serialiseAndNavigate(message);
});
// onMessage: When the app is open and it receives a push notification
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("onMessage data: ${message.data}");
});
// replacement for onResume: When the app is in the background and opened directly from the push notification.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('onMessageOpenedApp data: ${message.data}');
_serialiseAndNavigate(message);
});
我使用下面的代码行进行 flutter 通知配置的 firebase 消息配置,但现在在集成到最新版本的 firebase 消息后它给我错误
代码行
messaging.configure(onMessage: (Map<String, dynamic> message){}
DART 分析错误
error: The method 'configure' isn't defined for the type 'FirebaseMessaging'.
请检查以下示例。
@override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
}
FirebaseMessaging.configure() 被 firebase 团队删除:
原因:
如果多次调用 configure()
的先前实现会导致意外的副作用(注册不同的处理程序或删除处理程序)。此更改允许开发人员更明确地注册和删除处理程序,而不会通过 Streams 影响其他人。
使用FirebaseMessaging.onMessage
方法获取消息
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
});
使用 FirebaseMessaging.onMessageOpenedApp
替代 onLaunch
和 onResume
处理程序。
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(context, '/message',
arguments: MessageArguments(message, true));
});
基于 Jitesh 的回答,对我来说,需要实施 getInitialMessage
才能在应用程序终止时使导航正常工作(替换 onLaunch)
// workaround for onLaunch: When the app is completely closed (not in the background) and opened directly from the push notification
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
print('getInitialMessage data: ${message.data}');
_serialiseAndNavigate(message);
});
// onMessage: When the app is open and it receives a push notification
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("onMessage data: ${message.data}");
});
// replacement for onResume: When the app is in the background and opened directly from the push notification.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('onMessageOpenedApp data: ${message.data}');
_serialiseAndNavigate(message);
});