OneSignal / OneSignal-iOS-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your native iOS app with OneSignal. https://onesignal.com
Other
495 stars 263 forks source link

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

Open playerx opened 5 hours ago

playerx commented 5 hours ago

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:

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

playerx commented 5 hours ago

One note:

When the app is running, the push notification itself is received from OneSignal in the event, but it's not displayed in iOS. This code doesn't do anything: event.notification.display()

playerx commented 3 hours ago

Few observations:

  1. If I comment event.preventDefault() line, and keep only event.notification.display(), I get the following error:

    ERROR: OSNotificationWillDisplayEvent.notification.display cannot be called due to timing out or notification was already displayed.
  2. onWillDisplay is triggered twice on one push notification.