onLaunch running in loop

Viewed 182

I'm using Firebase Cloud Messaging for my app. What works is -

  1. onMessage working fine
  2. onLaunch & onResume triggers every time I push notification through console but...

What doesn't work is:

Every time I navigate to different screens, onLaunch gets called again and render the same data as before. How can I stop triggering it as configuration has been done in initState?

// 1. SETTINGS FOR PUSH NOTIFICATIONS
  FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      new FlutterLocalNotificationsPlugin();
  NotificationAppLaunchDetails notificationAppLaunchDetails;

  @override
  void initState() {
    super.initState();
    // PushNotificationsManager().init();

    // 2. SETTINGS FOR PUSH NOTIFICATIONS
    var android = new AndroidInitializationSettings('mipmap/ic_launcher');
    var ios = new IOSInitializationSettings();
    var platform = new InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin.initialize(platform);

    // 3. Configuration for FCM
    firebaseMessaging.configure(
      // This works fine ALL THE TIME
      onMessage: (Map<String, dynamic> msg) {
        showNotification(msg);
        print("onMessage called ${(msg)}");
      },

      // This works fine FIRST TIME BEFORE NAVIGATING BETWEEN SCREENS
      onResume: (Map<String, dynamic> msg) {
        userLaunchApp(msg);
        print("onMessage called ${(msg)}");
      },

      // This works fine FIRST TIME BEFORE NAVIGATING BETWEEN SCREENS
      onLaunch: (Map<String, dynamic> msg) {
        userLaunchApp(msg);
        print(" onLaunch called ${(msg)}");
      },


    );
    firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, alert: true, badge: true));
  }

Handling onMessage -

// Show notifications when submitted manually
  showNotification(Map<String, dynamic> msg) async {
    var android = new AndroidNotificationDetails(
      'Channel id',
      "Channel name",
      "Channel desc",
    );
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
        0, msg["notification"]["title"], msg["notification"]["body"], platform);
  }
//Handling onLaunch & onResume -
userLaunchApp(Map<String, dynamic> msg) async {
    setState(() {
      // Change flag value
      openedFromPush = true;

      // SET NOTIFICATION DATA AND SHOW
    });
  }

I have been unable to find an answer to this.

0 Answers
Related