skip to Main Content

I’ve this problem. I’ve a flutter app which is connected to firebase. When I run the app in release mode with the iphone connected it works fine. The app show splashscreen and after that show the home or the login.
The problem appear when I close/kill the app and I re-open it from the phone. In this case the app is freezed in the splashscreen. The same problem happen when I download the app from testflight. Someone can help me to understand which is the problem or a possible solution? Or a way to understand the problem?

This is the code about the main file:

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:device_preview/device_preview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_statusbarcolor_ns/flutter_statusbarcolor_ns.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'app_colors.dart';
import 'home/home_view.dart';
import 'main_binding.dart';
import 'app_pages.dart';
import 'locales.dart';

const BASE_URL = "https://aaa.bbb.com/api/v1"; // prod

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

const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'notification_channel', // id
  'Machine Notifications', // title
  //'This channel is used for important machine notifications.', // description
  importance: Importance.high,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

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

  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  String? tokenFBM = await FirebaseMessaging.instance.getToken();
  print(tokenFBM);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    FlutterStatusbarcolor.setStatusBarColor(AppColors.primaryBlue);
    FlutterStatusbarcolor.setStatusBarWhiteForeground(true);
    FirebaseMessaging.instance.requestPermission();

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      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: 'ic_stat_notification',
                color: AppColors.primaryBlue,
              ),
            ));
      }
    });


    return GetMaterialApp(
      locale: Get.deviceLocale,
      builder: DevicePreview.appBuilder,
      initialBinding: MainBinding(),
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('it', ''),
      ],
      translations: AppTranslation(),
      initialRoute: HomeView.ROUTE,
      fallbackLocale: Locale('it'),
      theme: ThemeData(
        primaryColor: AppColors.primaryBlue,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
      ),
      getPages: AppPages.getRoutes(),
      title: 'Title',
    );
  }
}```

This is flutter doctor:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.3, on macOS 13.1 22C65 darwin-arm64, locale en-GB)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] Android Studio (version 2022.2)
[✓] Android Studio (version 2021.3)
[✓] IntelliJ IDEA Community Edition (version 2022.3.2)
[✓] VS Code (version 1.77.3)
[✓] Connected device (3 available)
[✓] Network resources

2

Answers


  1. Check by commenting on the line where FirebaseMessaging.instance.getToken() is called. It may be the cause or try to do it asynchronously like this

    FirebaseMessaging.instance.getToken().then((value) { print("Token: $value"); });

    Login or Signup to reply.
  2. Firebase not initializing in IOS properly thats why splash screen freeze
    Please verify google_infoPlist.json added in IOS project

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search