Ali Alqallaf
Ali Alqallaf

Reputation: 1027

Duplicate notifications received using OneSignal flutter

I integrated onesignal notifications on my Flutter app, and everything works as expected but I receive duplicate notifications. Specifically, when the app is opened (in the foreground), I receive the notification in the app and display a widget for that, but I also receive the same notification with the phone's notification.

Here's my code to receive the notifications in the foreground

// this method is being called in main()
// show notification when the app is opened
    OneSignal.shared
        .setNotificationWillShowInForegroundHandler(_localNotificationsHandler);
  static _localNotificationsHandler(OSNotificationReceivedEvent event) {
    AppSnackbar.openNotificationsSnackbar(
      title: event.notification.title ?? '',
      msg: event.notification.body ?? '',
    );
  }

And this is the code for pushing notifications


  static Future<void> sendNotification({
    required List<String> tokens,
    required String title,
    required String message,
  }) async {
    try {
      var notification = OSCreateNotification(
        playerIds: tokens,
        content: message,
        heading: title,
      );

      await OneSignal.shared.postNotification(notification);
    } catch (e) {
      print(e);
    }
  }

Upvotes: 2

Views: 572

Answers (1)

Ali Alqallaf
Ali Alqallaf

Reputation: 1027

the solution is to add this line of code after handling the notification received on the app

event.complete(null);

Upvotes: 0

Related Questions