class 'FirebaseMessaging' 没有默认构造函数 Flutter 2

The class 'FirebaseMessaging' doesn't have a default constructor Flutter 2

在切换到 Flutter 2 之前,我使用的是旧版本的 firebaseMessaging,没有任何问题,现在我进行了最新的 version.After 升级,但出现以下错误:

The class 'FirebaseMessaging' doesn't have a default constructor.


并且:

The method 'configure' isn't defined for the type 'FirebaseMessaging'.


完整 class:


class ShowNotifications {
static final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
 static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  new FlutterLocalNotificationsPlugin();

  static void initialization(){
    var initializationSettingsAndroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }




 static void showNotification(String title, String body) async {
    await _demoNotification(title, body);
  }

 static Future<void> _demoNotification(String title, String body) async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'channel_ID', 'channel name', 'channel description',
        importance: Importance.max,
        playSound: true,
        // sound: 'sound',
        // sound: true,
        showProgress: true,
        priority: Priority.high,
        ticker: 'test ticker');

    var iOSChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        android: androidPlatformChannelSpecifics, iOS: iOSChannelSpecifics);
    await flutterLocalNotificationsPlugin
        .show(0, title, body, platformChannelSpecifics, payload: 'test');
  }


 static Future onSelectNotification(String payload) async {
    showDialog(
   //   context: context,
      builder: (_) {
        return new AlertDialog(
          title: Text("PayLoad"),
          content: Text("Payload : $payload"),
        );
      },
    );
  }


 static notification(){
    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        showNotification(message['notification']['title'], message['notification']['body']);
        // print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");

      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");

      },

    );
  }
}

我能够从所有应用程序页面控制此 class。

我需要用新版本改变什么。这样我就可以像过去一样使用class。

尝试更新您的 firebase_messaging 包,并注意为其他 Firebase SDK 使用兼容的包。我建议您从官方 documentation 复制那些您使用的每一个。

这是新的 Firebase Messaging SDK 如何与新的 API:

一起工作的完整示例
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../constants.dart';

FirebaseMessaging messaging = FirebaseMessaging.instance;

final _database = FirebaseDatabase.instance;
final _firestore = FirebaseFirestore.instance;
final _auth = FirebaseAuth.instance;

class MessagingService {
  static bool showToast = true;
  static String currentToken;

  static void initialize() async {
    await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        print('Message also contained a notification: ${message.notification}');
      }

      if (showToast && message.notification != null) {
        Fluttertoast.showToast(
          msg: "${message.notification.title}: ${message.notification.body} ",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 1,
          //backgroundColor: Colors.red,
          //textColor: Colors.white,
          fontSize: 16.0,
        );
      }
    });

    messaging.onTokenRefresh.listen((String token) async {
      await syncToken();
    });

    await syncToken();
  }

  static Future<String> getToken() async {
    String token = await messaging.getToken();
    return token;
  }

  static Future syncToken() async {
    try {
      String token = await messaging.getToken();
      if (token != currentToken) {
        if (syncDatabase == databases.RealtimeDatabase) {
          await _database
              .reference()
              .child(
                  '$kNotificationTokensSyncBasePath${_auth.currentUser.uid}/$token')
              .set(true);
        } else {
          await _firestore
              .doc('$kNotificationTokensSyncBasePath${_auth.currentUser.uid}')
              .set({'$token': true}, SetOptions(merge: true));
        }

        currentToken = token;
      }
    } catch (e) {
      print(e);
    }

    return;
  }

  static Future unsyncToken(User user) async {
    try {
      String token = await messaging.getToken();

      if (syncDatabase == databases.RealtimeDatabase) {
        await _database
            .reference()
            .child('$kNotificationTokensSyncBasePath${user.uid}/$token')
            .set(null);
      } else {
        await _firestore
            .doc('$kNotificationTokensSyncBasePath${_auth.currentUser.uid}')
            .set({'$token': FieldValue.delete()}, SetOptions(merge: true));
      }

      currentToken = null;
    } catch (e) {
      print(e);
    }

    return;
  }
}