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]: event.preventDefault() doesn't work on iOS #1592

Open
2 of 3 tasks
kamil-floqsta opened this issue Oct 26, 2023 · 5 comments
Open
2 of 3 tasks

[Bug]: event.preventDefault() doesn't work on iOS #1592

kamil-floqsta opened this issue Oct 26, 2023 · 5 comments

Comments

@kamil-floqsta
Copy link

kamil-floqsta commented Oct 26, 2023

What happened?

I would like to not display notification if some conditions are met.

Regarding to the [User Model] Rename Events & API Changes pull request:

OneSignal.Notifications.addEventListener('foregroundWillDisplay', (event) => {
      event.preventDefault();
      // async work
      event.getNotifications.display();
};

In the previous versions this could to achived by calling notificationReceivedEvent.complete(undefined)

Steps to reproduce?

1. Install react-native-onesignal 5.0.2
2. Build and run iOS
3. Subscribe user to the push notifications with OneSignal SDK
4. Add foregroundWillDisplay event listener and comment `event.getNotifications.display()`
5. Send test notification
6. The notification is displayed but should be hidden

What did you expect to happen?

I would like to not display notification if some conditions are met.

React Native OneSignal SDK version

5.0.2

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
@emawby
Copy link
Contributor

emawby commented Oct 26, 2023

@kamil-floqsta In order to not display the notification you just call event.preventDefault();
If you would then like to display the notification after delaying the default display you call event.getNotifications.display();
If you never want to show the notification do not call display

@kamil-floqsta
Copy link
Author

kamil-floqsta commented Oct 26, 2023

@kamil-floqsta In order to not display the notification you just call event.preventDefault();
If you would then like to display the notification after delaying the default display you call event.getNotifications.display();
If you never want to show the notification do not call display

@emawby Unfortunately it doesn't work. Here is a code sample:

OneSignal.Notifications.addEventListener(
    'foregroundWillDisplay',
    (notificationReceivedEvent: NotificationWillDisplayEvent) => {
      const notification = notificationReceivedEvent.getNotification();
        if (
          // some condition
        ) {
          notificationReceivedEvent.preventDefault();
          return;
        }
      }
      notification.display();
      }
  );

The listener receives a notification, then notificationReceivedEvent.preventDefault() is called but after about ~10 seconds the notification is displayed anyway.

@kamil-floqsta
Copy link
Author

@emawby I turned on logs with LogLevel.Verbose
And here is the Xcode outcome for the notification with preventDefault():

2023-10-26 21:32:17.300081+0200 PROJECT_NAME[85270:18154602] VERBOSE: OSNotificationWillDisplayEvent.preventDefault called.

2023-10-26 21:32:42.256768+0200 PROJECT_NAME[85270:18154435] WARNING: OSNotificationLifecycleListener:onWillDisplayNotification timed out. Display was not called within 25.000000 seconds. Continue with display notification: 0
2023-10-26 21:32:42.263611+0200 PROJECT_NAME[85270:18154435] VERBOSE: finishProcessingNotification: Fired!
2023-10-26 21:32:42.263806+0200 PROJECT_NAME[85270:18154435] VERBOSE: Notification display type: 0
2023-10-26 21:32:42.264041+0200 PROJECT_NAME[85270:18154435] VERBOSE: notificationReceived called! opened: NO
2023-10-26 21:32:42.264601+0200 PROJECT_NAME[85270:18154435] VERBOSE: finishProcessingNotification: call completionHandler with options: 0

@kamil-floqsta
Copy link
Author

@emawby Even if e. preventDefault() is removed the loggs says it was called:

2023-10-27 12:10:15.873749+0200 PROJECT_NAME[86156:18319475] VERBOSE: OSNotificationWillDisplayEvent.preventDefault called.

@kamil-floqsta kamil-floqsta changed the title [Bug]: event.getNotifications.display() doesn't work on iOS [Bug]: event.preventDefault() doesn't work on iOS Nov 3, 2023
@rpinto18
Copy link

rpinto18 commented Jan 8, 2024

@kamil-floqsta hello, any solution?

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

3 participants