【问题标题】:Firebase Flutter upgrade problem for firebase messagingFirebase 消息传递的 Firebase Flutter 升级问题
【发布时间】:2021-06-12 03:24:54
【问题描述】:

我已将 firebase 从 ^7.0.0 升级到 ^8.0.0-dev.15,现在出现错误,不知道如何修复。

这是我的遗留工作代码:

void initFirebase() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {});
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      setState(() {
        _notificationToken = token;
      });
    });
  }

我收到以下错误:

lib/files/signin.dart:52:7: Error: No named parameter with the name 'onLaunch'.
      onLaunch: (Map<String, dynamic> message) async {
      ^^^^^^^^
lib/files/signin.dart:62:24: Error: The getter 'onIosSettingsRegistered' isn't defined for the class 'FirebaseMessaging'.
 - 'FirebaseMessaging' is from 'package:firebase_messaging/firebase_messaging.dart' ('/C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-8.0.0-dev.15/lib/firebase_messaging.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'onIosSettingsRegistered'.
    _firebaseMessaging.onIosSettingsRegistered
                       ^^^^^^^^^^^^^^^^^^^^^^^

现在,我开始按照官方文档修复代码,但无法完全正确,感谢您的帮助:

  void initFirebase() {
    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) {
        print("onMessage: $message");
      });
      FirebaseMessaging.onMessageOpenedApp.listen(
      (RemoteMessage message) {
        print("onMessageOpenedApp: $message");
      });
    
    NotificationSettings settings = messaging.requestPermission (
        sound: true, badge: true, alert: true, provisional: true,
    );
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {});
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      setState(() {
        _notificationToken = token;
      });
    });
  }

【问题讨论】:

    标签: firebase flutter firebase-cloud-messaging


    【解决方案1】:

    如果您使用的是 firebase_messaging:^8.0.0-dev.15,您还应该添加 firebase_core:^0.7.0。而且没有“OnResume”、“onLaunch”、“onMessage”,还有其他选择。这是this answer 中提到的在这种情况下使用它的方法。更多内容可以查看official doc

    【讨论】:

    • 没有任何效果
    【解决方案2】:

    抱歉,我之前回答了另一个问题。此外,我使用了 Flutter 和 Firebase 的另一个主要版本。

    所以我删除了我的答案并在这里打开了一个新帖子:Flutter 2.0 with Firebase Cloud Messaging: onMessage not called on Android

    -- 编辑

    此错误是否与您的问题有关? https://github.com/FirebaseExtended/flutterfire/issues/4054

    问候,

    呜呜

    【讨论】:

    【解决方案3】:

    此旧版升级有一些重大更改。我重现了你的问题。 onresume、onlaunch 等概念已重新分类为前台消息和后台消息。

    要处理后台消息,请在 main.dart 文件等顶级类中实例化 firebase 消息。

    Future<void>main() async{
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
      runApp(MyApp());
    
    }
    
    

    在这种情况下创建一个方法 (_firebaseMessagingBackgroundHandler)。 (如果您要在后台使用其他 Firebase 服务,例如 Firestore,请不要忘记在使用其他 Firebase 服务之前致电initializeApp。)。

    背景

    Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      await Firebase.initializeApp();
      print("Handling a background message: ${message.messageId}");
      print(message.data);
    }
    
    

    前台通知略有不同。默认情况下,它们不会在屏幕上弹出。您可以使用以下方法访问前台通知

    
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print("Handling a foreground message ${message.notification.body}");
    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,
                icon: android?.smallIcon,
                // other properties...
              ),
            ));
      }
    });
    

    您可以使用 JSON 格式访问消息。

    onLaunch: 和 onResume:

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      print("app launched or resumed!!");
    });
    
    

    上面的两个代码将分别显示通知。后台处理程序还涵盖终止应用的场景。

    FirebaseMessaging.onMessageOpenedApp 返回一个 [Stream],当用户按下通过 FCM 显示的通知时调用它。

    PS:在使用 PostMan 等非 Google 服务器时,另一个重大变化是需要 Oauth-2 Bearer 令牌(短期存在,可以使用服务帐户从 https://developers.google.com/oauthplayground 获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 2021-02-19
      • 1970-01-01
      • 2021-09-04
      • 2020-05-24
      • 2018-09-07
      • 2021-12-12
      • 2021-07-21
      相关资源
      最近更新 更多