Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_messaging]: Double Notifications in Firebase onMessage Listener #13479

Closed
1 task done
nawafAlkhadidi opened this issue Oct 8, 2024 · 2 comments
Closed
1 task done
Labels
Needs Attention This issue needs maintainer attention. resolution: duplicate This issue or pull request already exists type: bug Something isn't working

Comments

@nawafAlkhadidi
Copy link

nawafAlkhadidi commented Oct 8, 2024

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Messaging

Which platforms are affected?

iOS

Description

I’m experiencing an issue where double notifications are being triggered in my Flutter app when using Firebase Cloud Messaging (FCM). This happens when handling the FirebaseMessaging.onMessage listener. Specifically, the issue occurs in the following scenario:

1 - Foreground: When the app is open, I’m manually displaying the notification using onMessage. However, the notification is being shown twice.

2 - Background: When the app is in the background, only one notification is shown, as expected.

my code :

final Set<String> hiddenChats = {};
class NotificationFCMUtils {
  Future init() async {
    try {
      await FirebaseMessaging.instance.requestPermission();
      await FirebaseMessaging.instance.subscribeToTopic("xx");
      await FirebaseMessaging.instance.subscribeToTopic("xx");
    } catch (e) {
      //   print(e);
    }
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      final String? type = message.data['type'] ?? "";
      final String? id = message.data['id'] ?? "0";
      final RemoteNotification? notification = message.notification;
      if (notification != null) {
        locator.get<LocalNotificationsManager>().showNotification(
              title: message.notification?.title,
              body: message.notification?.body,
              payLoad: type ?? "no",
              id: int.parse(id!),
            );
      }
    });
  }
  Future onMessageOpenedApp() async {
    try {
      RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
      if (initialMessage != null) {
        final String? type = initialMessage.data['type'] ?? "";
        final String? id = initialMessage.data['id'] ?? "0";
        move(id: id, type: type);
      }
      FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
        final String? type = message.data['type'] ?? "";
        final String? id = message.data['id'] ?? "0";
        move(id: id, type: type);
      });
    } catch (e) {
      throw Exception(e.toString());
    }
  }}

Reproducing the issue

1.	Implement FirebaseMessaging.onMessage.listen((RemoteMessage message) {...}).
2.	Send a notification while the app is in the foreground.
3.	Observe that the notification is displayed twice.

Firebase Core version

3.6.0

Flutter Version

3.24.1

Relevant Log Output

No response

Flutter dependencies

Expand Flutter dependencies snippet
Replace this line with the contents of your `flutter pub deps -- --style=compact`.

Additional context and comments

firebase_core: ^3.6.0
firebase_messaging: ^15.1.3

@nawafAlkhadidi nawafAlkhadidi added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Oct 8, 2024
@moo-kesh
Copy link

moo-kesh commented Oct 8, 2024

I am experiencing the same issue on iOS 18.1 devices (not sure about 18.0), but it works fine on devices using iOS versions below 18.

@SelaseKay
Copy link
Contributor

Duplicate of: 13366

@SelaseKay SelaseKay added the resolution: duplicate This issue or pull request already exists label Oct 8, 2024
@firebase firebase locked and limited conversation to collaborators Nov 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Attention This issue needs maintainer attention. resolution: duplicate This issue or pull request already exists type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants