init static method

Future<void> init()

Implementation

static Future<void> init() async {
  try {
    await Firebase.initializeApp();

    // Request permission
    FirebaseMessaging messaging = FirebaseMessaging.instance;
    await messaging.requestPermission(alert: true, badge: true, sound: true);

    // Set background handler
    FirebaseMessaging.onBackgroundMessage(
      _firebaseMessagingBackgroundHandler,
    );

    // Handle Foreground Messages
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        // If the message contains a notification, we can optionally show it locally
        // using our Local Notification system (if we want to force heads-up etc.)
        NotiBee.show(
          title: message.notification!.title ?? 'New Message',
          body: message.notification!.body ?? '',
          // We could parse custom data for icon/sound if sent in payload
        );
      }
    });

    // Handle message open app
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      debugPrint("Message clicked!");
    });
  } catch (e) {
    debugPrint("FCM Initialization failed: $e");
    debugPrint(
      "Did you add google-services.json / GoogleService-Info.plist?",
    );
  }
}