Skip to content

[Bug]: addForegroundLifecycleListener & event.notification.display() doesn't work as expected. #1510

@playerx

Description

@playerx

What happened?

No push notification received when the App is in Focus, addForegroundLifecycleListener & event.notification.display() have no effect.

Also tried zero ForegroundLifecycleListener with no luck, still no Push Notification displayed when the app is focused.

If the app is minimized or closed, I receive push notification from OneSignal successfully.

I'm upgrading project from older version SDK and replacing the following code:

OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;

with:

OneSignal.Notifications.addForegroundLifecycleListener(self);
...

func onWillDisplay(event: OSNotificationWillDisplayEvent) {
      print(">>> received notification");
      event.preventDefault()
      event.notification.display()
}

Please check the full example in Steps to reproduce.

Steps to reproduce?

1. Prepare a new iOS Capacitor project based on these instructions: https://documentation.onesignal.com/docs/ionic-capacitor-cordova-sdk-setup#2-ios-setup
2. Add `OneSignalXCFramework 5.2.6` Package Dependency (also tried `5.2.5` as well with no luck)
3. Use the following code in `AppDelegate.swift`:

import UIKit
import Capacitor
import OneSignalFramework

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, OSNotificationLifecycleListener {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        OneSignal.initialize("XXX", withLaunchOptions: launchOptions);
        OneSignal.login("XXX");
        OneSignal.Notifications.addForegroundLifecycleListener(self);
     
        return true
    }
  
    func onWillDisplay(event: OSNotificationWillDisplayEvent) {
      print(">>> received notification");
      event.preventDefault()
      event.notification.display()
    }


    func applicationWillResignActive(_ application: UIApplication) {
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    }

    func applicationWillTerminate(_ application: UIApplication) {
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        return ApplicationDelegateProxy.shared.application(application, continue: userActivity, restorationHandler: restorationHandler)
    }

}

What did you expect to happen?

My expectation was to receive Push Notifications, even if the app is Active. But the result is following:

  • If the app is in background or closed, I received Push notifications from OneSignal
  • If the app is running, the onWillDisplay event was fired with push notification data, but the push notification itself isn't there. No error, no push notification.

OneSignal iOS SDK version

5.2.6 (also tried 5.2.5)

iOS version

13

Specific iOS version

* iOS 18.0.1 (Minimum Deployment Target: iOS 13.0)

Relevant log output

No error logs, everything seems fine in logs.

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