Home > other >  Firebase Push Notification in Flutter when App Kiled
Firebase Push Notification in Flutter when App Kiled

Time:10-20

I'm having an issue dealing with Firebase push notifications when the App is KILLED. What's happening: First of all, push notifications should work like this. When you tap, you are redirected to a Move-Type Job or an Event-Type Job.

  • When app is in BACKGROUND MODE, push notification shows as it should, and it redirects to the page that it should.
  • When App is KILLED, push notifications still shows, but when you tap on them you are not redirected, you are just opening the App.

Future initialize() async {
    await getConnectivity();
    if (hasInternet) {
      try {
        await configurePushNotificationHandlers();
      } catch (e) {
        hasApiConnection = false;
      }
    }
  }

Future configurePushNotificationHandlers() async {
    await navigationService.navigateReplacementWithParams(ErrorPage());

    await _firebaseMessaging.requestPermission(
      alert: true,
      badge: true,
      provisional: false,
      sound: true,
    );

    String fbToken = await _firebaseMessaging.getToken();
    FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
      if (message.data != null && message.data.containsKey('showInPageNotification')) {
        numberOfNotifications  ;
      }
      notifyListeners();
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      print('A new onMessageOpenedApp event was published!');
      if (message.data != null && message.data.containsKey('job_id')) {
        String jobId = message.data['job_id'];
        String jobType = message.data['job_type'];

        jobType = jobType.toLowerCase();

        if (jobType == 'move') {
          await goToJobDetail(jobId);
        } else {
          await goToEventDetail(jobId);
        }
      }
    });

Anyone has a clue of why does this happens? Push notifications are working fine, it's the redirection the current ISSUE. Thanks!

CodePudding user response:

Use FirebaseMessaging.instance.getInitialMessage() method to get messages

If App is Closed/Killed

FirebaseMessaging.instance
          .getInitialMessage()
          .then((RemoteMessage message) {
      print("FirebaseMessaging.getInitialMessage $message");
   });

Try this and let me know.

  • Related