react-native-onesignal
react-native-onesignal copied to clipboard
[Bug]: event.preventDefault() doesn't work on iOS
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?
- [X] iOS
- [ ] Android
Relevant log output
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
@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 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 callevent.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.
@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
@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 hello, any solution?