Skip to content

Commit ad2425d

Browse files
committed
Add the notification discard parameter to the NotificationReceivedEvent
This is identical to the NotificationWillDisplay event parameter and logic in the generationProcessor
1 parent eee4d7b commit ad2425d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/notifications/INotificationReceivedEvent.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,11 @@ interface INotificationReceivedEvent {
5050
* can still be manually displayed using `notification.display()`.
5151
*/
5252
fun preventDefault()
53+
54+
/**
55+
* Call this to prevent OneSignal from displaying the notification automatically.
56+
* @param discard an [preventDefault] set to true to dismiss the notification with no
57+
* possibility of displaying it in the future.
58+
*/
59+
fun preventDefault(discard: Boolean)
5360
}

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/NotificationReceivedEvent.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ internal class NotificationReceivedEvent(
99
override val notification: Notification,
1010
) : INotificationReceivedEvent {
1111
var isPreventDefault: Boolean = false
12+
var discard: Boolean = false
1213

1314
override fun preventDefault() {
15+
preventDefault(false)
16+
}
17+
18+
override fun preventDefault(discard: Boolean) {
1419
Logging.debug("NotificationReceivedEvent.preventDefault()")
1520
isPreventDefault = true
21+
this.discard = discard
1622
}
1723
}

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/generation/impl/NotificationGenerationProcessor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ internal class NotificationGenerationProcessor(
7373
GlobalScope.launch(Dispatchers.IO) {
7474
_lifecycleService.externalRemoteNotificationReceived(notificationReceivedEvent)
7575

76-
if (notificationReceivedEvent.isPreventDefault) {
76+
if (notificationReceivedEvent.discard) {
77+
wantsToDisplay = false
78+
} else if (notificationReceivedEvent.isPreventDefault) {
7779
// wait on display waiter. If the caller calls `display` on the notification,
7880
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback
7981
// never calls `display` we will timeout and never set `wantsToDisplay` to true.
@@ -106,7 +108,7 @@ internal class NotificationGenerationProcessor(
106108
_lifecycleService.externalNotificationWillShowInForeground(notificationWillDisplayEvent)
107109

108110
if (notificationWillDisplayEvent.discard) {
109-
wantsToDisplay = false;
111+
wantsToDisplay = false
110112
} else if (notificationWillDisplayEvent.isPreventDefault) {
111113
// wait on display waiter. If the caller calls `display` on the notification,
112114
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback

0 commit comments

Comments
 (0)