title: How to display notification in app in flutter
body: <p>I'm trying to display the background and foreground notification in flutter using <code>flutter_local_notifications</code> package. here is my firebase class code</p>
  <pre><code>class FirebaseMessagingService {
    final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
    IOSInitializationSettings? _initializationSettingsIOS;
    AndroidInitializationSettings? _initializationSettingsAndroid;
    AndroidNotificationDetails? _androidLocalNotificationDetails;
    AndroidNotificationChannel? androidNotificationchannel;
    static FirebaseMessagingService? _messagingService;
    NotificationDetails? _androidNotificationDetails;
    InitializationSettings? _initializationSettings;
    //NotificationNavigationClass _notificationNavigationClass = NotificationNavigationClass();
    NotificationAppLaunchDetails? _notificationAppLaunchDetails;
    bool? _didNotificationLaunchApp;
    static FirebaseMessaging? _firebaseMessaging;
    FirebaseMessagingService._createInstance();
  
    factory FirebaseMessagingService() {
      // factory with constructor, return some value
      if (_messagingService == null) {
        _messagingService = FirebaseMessagingService
            ._createInstance(); // This is executed only once, singleton object
        _firebaseMessaging = _getMessagingService();
      }
      return _messagingService!;
    }
  
    static FirebaseMessaging _getMessagingService() {
      return _firebaseMessaging ??= FirebaseMessaging.instance;
    }
  
    Future&lt;String?&gt; getToken() {
      return _firebaseMessaging!.getToken();
    }
  
    Future initializeNotificationSettings() async
    {
      NotificationSettings? settings = await _firebaseMessaging
          ?.requestPermission(
        alert: true,
        announcement: false,
        badge: true,
        carPlay: false,
        criticalAlert: false,
        provisional: false,
        sound: true,
      );
      if (settings?.authorizationStatus == AuthorizationStatus.authorized) {
        print('User granted permission');
      } else
      if (settings?.authorizationStatus == AuthorizationStatus.provisional) {
        print('User granted provisional permission');
      } else {
        print('User declined or has not accepted permission');
      }
  
      androidNotificationchannel = const AndroidNotificationChannel(
        NOTIFICATION_ID, // id
        NOTIFICATION_TITLE, // title
        description: NOTIFICATION_DESCRIPTION,
        // description
        importance: Importance.max,
      );
      //
      await _flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation&lt;
          AndroidFlutterLocalNotificationsPlugin&gt;()
          ?.createNotificationChannel(androidNotificationchannel!);
  
      if (Platform.isIOS) {
        //configure local notification for ios
        await _initializeIosLocalNotificationSettings();
      }
      else {
        //configure local notification for android
        await _initializeAndroidLocalNotificationSettings();
      }
    }
  
    Future&lt;void&gt; _initializeIosLocalNotificationSettings() async
    {
      _initializationSettingsIOS = const IOSInitializationSettings(
          requestAlertPermission: false,
          requestBadgePermission: false,
          requestSoundPermission: false
      );
      _initializationSettings = InitializationSettings(
          iOS: _initializationSettingsIOS
      );
  
    }
  
  
    Future&lt;void&gt; _initializeAndroidLocalNotificationSettings() async
    {
  
      _initializationSettingsAndroid =
          const AndroidInitializationSettings('mipmap/ic_launcher');
      _initializationSettings = InitializationSettings(
        android: _initializationSettingsAndroid,
      );
      _androidLocalNotificationDetails =
          const AndroidNotificationDetails(
              LOCAL_NOTIFICATION_ID,
              LOCAL_NOTIFICATION_TITLE,
              channelDescription: LOCAL_NOTIFICATION_DESCRIPTION,
              importance: Importance.max,
              priority: Priority.high);
      _androidNotificationDetails =
          NotificationDetails(android: _androidLocalNotificationDetails);
  
  
    //This will execute when the app is open and in foreground
    void foregroundNotification() {
     
        try {
          _showLocalNotification(localNotificationId: DateTime
              .now()
              .hour + DateTime
              .now()
              .minute + DateTime
              .now()
              .second,
              notificationData: message?.data,
            messageTitle:message?.notification?.title ,
            messageBody: message?.notification?.body,
          );
        } catch (error) {
          log(&quot;error&quot;);
        }
      });
    }
    void _showLocalNotification(
        {int? localNotificationId, Map&lt;String, dynamic&gt;? notificationData,String? messageTitle,String? messageBody}) async
    {
      if (notificationData != null) {
        log(&quot;Notification Data:${notificationData}&quot;);
      
          await _flutterLocalNotificationsPlugin.show(
              localNotificationId ?? 0, notificationData[&quot;title&quot;]??messageTitle,
              notificationData[&quot;body&quot;]??messageBody,
              _androidNotificationDetails,
              payload: jsonEncode(notificationData));
  
      }
    }
  
  
    //This will excute when the app is in background but not killed and tap on that notification
    void backgroundTapNotification() {
      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) async {
        try {
          RemoteMessage? terminatedMessage = await _firebaseMessaging
              ?.getInitialMessage();
        } catch (error) {
          log(&quot;error&quot;);
        }
      });
    }
  
  }
  
  
  </code></pre>
  <p>and calling it in splash screen</p>
  <pre><code>@override
    void initState() {
      super.initState();
      controller = AnimationController(
          vsync: this, duration: const Duration(milliseconds: 500));
      heartbeatAnimation =
          Tween&lt;double&gt;(begin: 100.0, end: 250.0).animate(controller);
      controller.forward().whenComplete(() {
        controller.reverse();
      });
  
      
      void _setNotifications() async {
        FirebaseMessagingService().foregroundNotification();
        FirebaseMessagingService().backgroundTapNotification();
      }
  
        await FirebaseMessagingService().initializeNotificationSettings();
        _setNotifications();
  }
  
  
  </code></pre>
  <p>When i close the app and hit the API notification is displaying on background, but when i open the app and hit the API notification is not displaying, nor foreground and not on background.</p>
  <p>please help me to get out from this problem.</p>