Skip to content

[Bug]: addForegroundWillDisplayListener Triggers multiple times #1036

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

Open
2 of 3 tasks
EsinShadrach opened this issue May 16, 2025 · 0 comments
Open
2 of 3 tasks

[Bug]: addForegroundWillDisplayListener Triggers multiple times #1036

EsinShadrach opened this issue May 16, 2025 · 0 comments
Labels

Comments

@EsinShadrach
Copy link

What happened?

I have something basic for showing notifications within my app, but anytime I trigger a notification, the add foreground listener triggers multiple times, causing the notification, in this case Drops.show() to appear multiple times

static void listenForNotificationsForeground(BuildContext context) {
  OneSignal.Notifications.addForegroundWillDisplayListener((event) {
    event.preventDefault();

    debugPrint(event.jsonRepresentation());

    if (!context.mounted) {
      MyLogger.info("OneSignal: Navigator not mounted");
      return;
    }

    final OSNotification(:title, :body) = event.notification;

    Drops.show(
      context,
      title: title ?? "New Notification",
      subtitle: body,
      icon: IconsaxPlusBold.notification_bing,
      iconColor: context.colorScheme.primary,
    );
  });
}

Duplicate Notification Supressed i had to move it out of my service layer to a stateful widget to track the state

  void handleNotifs() {
    OneSignal.Notifications.addForegroundWillDisplayListener((event) {
      event.preventDefault();

      debugPrint(event.jsonRepresentation());

      final notificationId = event.notification.notificationId;
      final now = DateTime.now();

      // Debounce: suppress duplicates within 2 seconds
      if (_lastNotificationId == notificationId &&
          _lastNotificationTime != null &&
          now.difference(_lastNotificationTime!).inMilliseconds < 2000) {
        debugPrint("Duplicate notification suppressed");
        return;
      }

       setState(() {
          _lastNotificationId = notificationId;
          _lastNotificationTime = now;
        });


      if (!context.mounted) {
        MyLogger.info("OneSignal: Context not mounted");
        return;
      }

      final OSNotification(:title, :body) = event.notification;

      Drops.show(
        context,
        title: (title ?? "New Notification") + notificationId,
        subtitle: body,
        icon: IconsaxPlusBold.notification_bing,
        iconColor: context.colorScheme.primary,
      );
    });
  }

Image

Steps to reproduce?

on latest version trigger a notification from perhaps the dashboard with a listener

What did you expect to happen?

I expected the notification foreground listener to show only once

OneSignal Flutter SDK version

^5.3.2

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

I/flutter ( 4117): Duplicate notification suppressed
I/flutter ( 4117): {
I/flutter ( 4117):   "notification": "{
I/flutter ( 4117):   "google.delivered_priority": "high",
I/flutter ( 4117):   "google.sent_time": 1747365241107,
I/flutter ( 4117):   "google.ttl": 259200,
I/flutter ( 4117):   "google.original_priority": "high",
I/flutter ( 4117):   "custom": "{"i":"d87bd1f5-7d1d-49da-abec-806983a3e573","a":{}}",
I/flutter ( 4117):   "pri": "5",
I/flutter ( 4117):   "vis": "1",
I/flutter ( 4117):   "from": "514669514339",
I/flutter ( 4117):   "alert": "Sample stuff ex",
I/flutter ( 4117):   "title": "Example",
I/flutter ( 4117):   "google.message_id": "0:1747365241118352%d4cecd66f9fd7ecd",
I/flutter ( 4117):   "google.c.sender.id": "514669514339"
I/flutter ( 4117): }"
I/flutter ( 4117): }
I/flutter ( 4117): Duplicate notification suppressed
I/flutter ( 4117): {
I/flutter ( 4117):   "notification": "{
I/flutter ( 4117):   "google.delivered_priority": "high",
I/flutter ( 4117):   "google.sent_time": 1747365241107,
I/flutter ( 4117):   "google.ttl": 259200,
I/flutter ( 4117):   "google.original_priority": "high",
I/flutter ( 4117):   "custom": "{"i":"d87bd1f5-7d1d-49da-abec-806983a3e573","a":{}}",
I/flutter ( 4117):   "pri": "5",
I/flutter ( 4117):   "vis": "1",
I/flutter ( 4117):   "from": "514669514339",
I/flutter ( 4117):   "alert": "Sample stuff ex",
I/flutter ( 4117):   "title": "Example",
I/flutter ( 4117):   "google.message_id": "0:1747365241118352%d4cecd66f9fd7ecd",
I/flutter ( 4117):   "google.c.sender.id": "514669514339"
I/flutter ( 4117): }"
I/flutter ( 4117): }
I/flutter ( 4117): Duplicate notification suppressed
I/flutter ( 4117): {
I/flutter ( 4117):   "notification": "{
I/flutter ( 4117):   "google.delivered_priority": "high",
I/flutter ( 4117):   "google.sent_time": 1747365241107,
I/flutter ( 4117):   "google.ttl": 259200,
I/flutter ( 4117):   "google.original_priority": "high",
I/flutter ( 4117):   "custom": "{"i":"d87bd1f5-7d1d-49da-abec-806983a3e573","a":{}}",
I/flutter ( 4117):   "pri": "5",
I/flutter ( 4117):   "vis": "1",
I/flutter ( 4117):   "from": "514669514339",
I/flutter ( 4117):   "alert": "Sample stuff ex",
I/flutter ( 4117):   "title": "Example",
I/flutter ( 4117):   "google.message_id": "0:1747365241118352%d4cecd66f9fd7ecd",
I/flutter ( 4117):   "google.c.sender.id": "514669514339"
I/flutter ( 4117): }"
I/flutter ( 4117): }
I/flutter ( 4117): Duplicate notification suppressed
D/OneSignal( 4117): [main] NotificationWillDisplayEvent.preventDefault(false)

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant