Firebase 云消息传递无法在我的 Flutter 应用程序上运行?

问题描述 投票:0回答:1

我将 Firebase 设置到我的 Flutter 应用程序中,因为我正在使用 Firebase 数据库,一切都工作正常,但是当尝试从控制台发送通知时,没有任何显示,而后台的应用程序是我的主应用程序

void main() async {
  WidgetsFlutterBinding.ensureInitialized();


  try {
    Platform.isAndroid
        ? await Firebase.initializeApp(
            options: const FirebaseOptions(
              apiKey: "api",
              appId: "appid",
              messagingSenderId: "sendid",
              projectId: "projectid",
              storageBucket: "storagebucket",
            ),
          )
        : await Firebase.initializeApp(
            options: const FirebaseOptions(
                apiKey: "API",
                appId: "appid",
                messagingSenderId: "sendid",
                projectId: "projected",
                storageBucket: "storage",
                authDomain: "auth",
                measurementId: "measureid"),
          );
  } catch (e) {
    "Error$e";
  }

  await FirebaseAPI().initNotification();
  // Pass all uncaught "fatal" errors from the framework to Crashlytics
  FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
  // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  PlatformDispatcher.instance.onError = (error, stack) {
    FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
    return true;
  };

  await GetStorage.init();
  runApp(const MyApp());
  //runApp(DevicePreview(builder: (context) => const MyApp()));
}

这就是我的 firebase API

import 'package:firebase_messaging/firebase_messaging.dart';

class FirebaseAPI {
  final _firebaseMessaging = FirebaseMessaging.instance;

  Future<void> initNotification() async {
    await _firebaseMessaging.requestPermission();
    final fcmToken = await _firebaseMessaging.getToken();
    print("fcmtoken: ${fcmToken}");
    FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
  }

  Future<void> handleBackgroundMessage(RemoteMessage message) async {
    print("Title: ${message.notification?.title}");
    print("Body: ${message.notification?.body}");
    print("Payload: ${message.data}");
  }
}

这是我的应用程序 build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id("com.google.firebase.crashlytics")
    id("com.google.gms.google-services")
}

dependencies {
  implementation(platform("com.google.firebase:firebase-bom:32.7.2"))
  implementation("com.google.firebase:firebase-crashlytics")
  implementation("com.google.firebase:firebase-analytics")
  implementation "androidx.multidex:multidex:2.0.1"
}

当我使用 Firebase 控制台并使用设备令牌发送测试消息时,没有任何反应,有人可以帮助我吗?

flutter dart firebase-cloud-messaging
1个回答
0
投票

我使用 Firebase CLI 再次重新安装了 firebase,它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.