skip to Main Content

In my flutter project I have configured to received Firebase messages when the app is opened or closed.
But when the notification is triggered with the app closed, upon clicking in the notification and opening the app, the notification is triggered again. Is that a known issue or am I configuring something wrong?

@override
  void initState() {
    super.initState();

    _initFuture = initData();
    messaging = FirebaseMessaging.instance;
    _requestNotificationPermissions();

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        NotificationService().showNotification(message);
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        NotificationService().showNotification(message);
    });

  }

I tried using an approach suggested somewhere else about checking the messageId so it validates before continuinng , but that didn’t work out.

2

Answers


  1. You just have to add condition for the platform and remove the onMessageOpenedApp from there,

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (Platform.isAndroid) {
      if (message.notification != null) {
        NotificationController.createNewNotification(
            title: message.notification?.title,
            contentId: message.data["content_id"],
            type: message.data["type"],
            body: message.notification?.body);
      }
    }
    

    });

    Login or Signup to reply.
  2. FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
    NotificationService().showNotification(message); });

    Don’t call this NotificationService().showNotification(message)
    for now then check . you will get only time.

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