Skip to content

[Bug]: When the app is closed, notifications that were received while it was open do not open the app #977

Open
@AlexFlorenco

Description

@AlexFlorenco

What happened?

My problem only happens when I receive a notification with the app running (in the foreground or background). When you kill the app and click on the notification, nothing happens.
The notification just disappears.

I noticed that the same problem happens with the OneSignal example project here on github.
Is this a known limitation?

In all other situations, notifications work perfectly, in the foreground, background and dead.

Steps to reproduce?

class NotificationService {
  NotificationService._();
  static final NotificationService instance = NotificationService._();

  bool initialized = false;
  bool appInBackground = false;
  bool appOpenByNotification = false;

  Future<void> initialize() async {
    OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
    OneSignal.initialize("MY_APP_ID");
    OneSignal.LiveActivities.setupDefault();
    OneSignal.Notifications.clearAll();

    OneSignal.Notifications.addClickListener((event) {
      if (appInBackground) {
        appOpenByNotification = true;
        AdsService.loadAndShowNotificationAd(callback: () {
          handleNotificationReceived(event);
          appOpenByNotification = false;
        });
      } else if (initialized) {
        AdsService.loadAndShowNotificationAd(callback: () {
          handleNotificationReceived(event);
        });
      } else {
        appOpenByNotification = true;
        Timer.periodic(const Duration(milliseconds: 500), (timer) {
          if (initialized) {
            timer.cancel();
            handleNotificationReceived(event);
          }
        });
      }
    });

    OneSignal.Notifications.addForegroundWillDisplayListener((event) {
      event.preventDefault();
      event.notification.display();
    });
  }
}

What did you expect to happen?

I expected the closed app to start when clicking on the notification that was received when the app was running

OneSignal Flutter SDK version

5.2.8

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions