OneSignal-Flutter-SDK icon indicating copy to clipboard operation
OneSignal-Flutter-SDK copied to clipboard

[Bug]: addForegroundWillDisplayListener Triggers multiple times

Open EsinShadrach opened this issue 6 months ago • 0 comments
trafficstars

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
  • [x] 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

  • [x] I agree to follow this project's Code of Conduct

EsinShadrach avatar May 16 '25 03:05 EsinShadrach