如何实现Firebase Messaging flutter



我已经将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);
},
);
}

似乎找不到实现它的方法。有人能帮忙吗?

您应该执行以下操作:

  • 更改initialize方法的主体以包括FirebaseMessaging.onMessageFirebaseMessaging.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);
}

}

相关内容

  • 没有找到相关文章

最新更新