Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
playerx opened this issue Nov 1, 2024 · 5 comments

Comments

@playerx
Copy link

playerx commented Nov 1, 2024

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
@playerx
Copy link
Author

playerx commented Nov 1, 2024

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
Copy link
Author

playerx commented Nov 1, 2024

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.
  1. onWillDisplay is triggered twice on one push notification.

@nan-li
Copy link
Contributor

nan-li commented Nov 4, 2024

Hi @playerx thanks for reporting. Just to confirm, even when you don't call OneSignal.Notifications.addForegroundLifecycleListener(self), no push is displayed?

Can you share the Verbose-level logs starting from when the notification is supposed to be received by the device?
Also, you are modifying the native AppDelegate file even when you are using Capacitor?
Have you tested interacting with OneSignal via the cross-platform? Docs on this method for the different platforms here: https://documentation.onesignal.com/docs/mobile-sdk-reference#addforegroundlifecyclelistener

@playerx
Copy link
Author

playerx commented Nov 5, 2024

Hi @nan-li , that's correct even if I don't call addForegroundLifecycleListener no push is displayed when the app is active.
I started investigation because of that behavior, my expectation was that it should display push notification whether app is active or not.

Originally I've tried with capacitor-cordova plugin, next thought maybe there was an issue in that plugin and switched to iOS sdk directly, but the issue remains the same.

I'll enable Verbose level logs and share it 👌🏻

@playerx
Copy link
Author

playerx commented Nov 5, 2024

Initialization Logs:

VERBOSE: setAppId called with appId: 5934bf6c-8446-4fcb-baa9-be0c363afab9!
VERBOSE: setLaunchOptions() called with launchOptions: (null)!
VERBOSE: launchOptions is set and appId of 5934bf6c-8446-4fcb-baa9-be0c363afab9 is set, initializing OneSignal...
VERBOSE: In app message lifecycle listener added successfully
VERBOSE: In app message click listener added successfully
VERBOSE: Notification click listener added successfully
VERBOSE: OneSignal.User login called with externalId: ezeki
VERBOSE: OneSignalUserManager internal _login called with externalId: ezeki
VERBOSE: OneSignalUserManager.createNewUser: not creating new user due to logging into the same user.)
VERBOSE: OSOperationRepo flushDeltaQueue in background: false with queue: [<OSDelta OS_UPDATE_PROPERTIES_DELTA with property: session_count value: 1>]
VERBOSE: OSPropertyOperationExecutor enqueue delta <OSDelta OS_UPDATE_PROPERTIES_DELTA with property: session_count value: 1>
VERBOSE: OSPropertyOperationExecutor processDeltaQueue with queue: [<OSDelta OS_UPDATE_PROPERTIES_DELTA with property: session_count value: 1>]
VERBOSE: HTTP Request (OneSignalUser.OSRequestUpdateProperties) with URL: https://api.onesignal.com/apps/5934bf6c-8446-4fcb-baa9-be0c363afab9/users/by/onesignal_id/2ce963ec-8f3e-4d7c-b475-01d31327a07c, with parameters: {
  "refresh_device_metadata" : true,
  "deltas" : {
    "session_count" : 1
  }
}
VERBOSE: network response (OneSignalUser.OSRequestUpdateProperties) with URL https://api.onesignal.com/apps/5934bf6c-8446-4fcb-baa9-be0c363afab9/users/by/onesignal_id/2ce963ec-8f3e-4d7c-b475-01d31327a07c: {
    httpStatusCode = 202;
    properties =     {
        country = GE;
        ip = "94.43.43.44";
        "last_active" = 1730829680;
    };
    "ryw_delay" = 500;
    "ryw_token" = 10000000004476059766;
}
VERBOSE: OneSignal.LiveActivities executing outstanding requests

Logs after receiving a notification (when App was active):

VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler:
VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler: Fired! Test test
VERBOSE: finishProcessingNotification: Fired!
VERBOSE: Notification display type: 7
VERBOSE: notificationReceived called! opened: NO
VERBOSE: finishProcessingNotification: call completionHandler with options: 7
VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler:
VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler: Fired! Test test
VERBOSE: finishProcessingNotification: Fired!
VERBOSE: Notification display type: 7
VERBOSE: notificationReceived called! opened: NO
VERBOSE: finishProcessingNotification: call completionHandler with options: 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants