Search code examples
flutterdartfirebase-cloud-messaging

Firebase cloud messaging isn't working on my flutter app?


I set Firebase into my Flutter app as I'm using the Firebase database and everything is working fine but when trying to send a notification from the console nothing shows while the app on the background that's my main

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()));
}

and that's my 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}");
  }
}

and that's my app 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"
}

When I'm using the Firebase console and sending test messages with the device token nothing is happening can anyone help me please?


Solution

  • I reinstalled firebase again with Firebase CLI and it works fine.