Skip to content

Commit 7491122

Browse files
authored
Merge pull request #2138 from OneSignal/feat/call_prevent_default_multiple_times
Allow `preventDefault` to be fired multiple times to allow discarding later
2 parents ec71f7e + 9b7b91d commit 7491122

File tree

7 files changed

+190
-314
lines changed

7 files changed

+190
-314
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ interface INotificationReceivedEvent {
5353

5454
/**
5555
* Call this to prevent OneSignal from displaying the notification automatically.
56+
* This method can be called up to two times with false and then true, if processing time is needed.
57+
* Typically this is only possible within a short
58+
* time-frame (~30 seconds) after the notification is received on the device.
5659
* @param discard an [preventDefault] set to true to dismiss the notification with no
5760
* possibility of displaying it in the future.
5861
*/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ interface INotificationWillDisplayEvent {
4141

4242
/**
4343
* Call this to prevent OneSignal from displaying the notification automatically.
44+
* This method can be called up to two times with false and then true, if processing time is needed.
45+
* Typically this is only possible within a short
46+
* time-frame (~30 seconds) after the notification is received on the device.
4447
* @param discard an [preventDefault] set to true to dismiss the notification with no
4548
* possibility of displaying it in the future.
4649
*/

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.onesignal.notifications.internal
33
import androidx.core.app.NotificationCompat
44
import com.onesignal.common.safeJSONObject
55
import com.onesignal.common.safeString
6-
import com.onesignal.common.threading.Waiter
6+
import com.onesignal.common.threading.WaiterWithValue
77
import com.onesignal.core.internal.time.ITime
88
import com.onesignal.debug.internal.logging.Logging
99
import com.onesignal.notifications.BackgroundImageLayout
@@ -24,7 +24,11 @@ import org.json.JSONObject
2424
*/
2525
class Notification : IDisplayableMutableNotification {
2626
var notificationExtender: NotificationCompat.Extender? = null
27-
val displayWaiter: Waiter = Waiter()
27+
28+
/**
29+
* Wake with true to display the notification, or false to discard it permanently.
30+
*/
31+
val displayWaiter = WaiterWithValue<Boolean>()
2832

2933
override var groupedNotifications: List<Notification>? = null
3034
override var androidNotificationId = 0
@@ -251,7 +255,7 @@ class Notification : IDisplayableMutableNotification {
251255
}
252256

253257
override fun display() {
254-
displayWaiter.wake()
258+
displayWaiter.wake(true)
255259
}
256260

257261
/**

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ internal class NotificationReceivedEvent(
1616
}
1717

1818
override fun preventDefault(discard: Boolean) {
19-
Logging.debug("NotificationReceivedEvent.preventDefault()")
19+
Logging.debug("NotificationReceivedEvent.preventDefault($discard)")
20+
21+
// If preventDefault(false) has already been called and it is now called again with
22+
// preventDefault(true), the waiter is fired to discard this notification.
23+
// This is necessary for wrapper SDKs that can call preventDefault(discard) twice.
24+
if (isPreventDefault && discard) {
25+
notification.displayWaiter.wake(false)
26+
}
2027
isPreventDefault = true
2128
this.discard = discard
2229
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ internal class NotificationWillDisplayEvent(
1414
}
1515

1616
override fun preventDefault(discard: Boolean) {
17-
Logging.debug("NotificationWillDisplayEvent.preventDefault()")
17+
Logging.debug("NotificationWillDisplayEvent.preventDefault($discard)")
18+
19+
// If preventDefault(false) has already been called and it is now called again with
20+
// preventDefault(true), the waiter is fired to discard this notification.
21+
// This is necessary for wrapper SDKs that can call preventDefault(discard) twice.
22+
if (isPreventDefault && discard) {
23+
notification.displayWaiter.wake(false)
24+
}
1825
isPreventDefault = true
1926
this.discard = discard
2027
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ internal class NotificationGenerationProcessor(
7676
if (notificationReceivedEvent.discard) {
7777
wantsToDisplay = false
7878
} else if (notificationReceivedEvent.isPreventDefault) {
79-
// wait on display waiter. If the caller calls `display` on the notification,
80-
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback
81-
// never calls `display` we will timeout and never set `wantsToDisplay` to true.
8279
wantsToDisplay = false
83-
notification.displayWaiter.waitForWake()
84-
wantsToDisplay = true
80+
// wait on display waiter. If the caller calls `display` or `preventDefault(true)` on the notification,
81+
// we will exit `waitForWake` and set `wantsToDisplay` to true or false respectively. If the callback
82+
// never calls `display` or `preventDefault(true)`, we will timeout and never update `wantsToDisplay`.
83+
wantsToDisplay = notification.displayWaiter.waitForWake()
8584
}
8685
}.join()
8786
}
@@ -110,12 +109,11 @@ internal class NotificationGenerationProcessor(
110109
if (notificationWillDisplayEvent.discard) {
111110
wantsToDisplay = false
112111
} else if (notificationWillDisplayEvent.isPreventDefault) {
113-
// wait on display waiter. If the caller calls `display` on the notification,
114-
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback
115-
// never calls `display` we will timeout and never set `wantsToDisplay` to true.
116112
wantsToDisplay = false
117-
notification.displayWaiter.waitForWake()
118-
wantsToDisplay = true
113+
// wait on display waiter. If the caller calls `display` or `preventDefault(true)` on the notification,
114+
// we will exit `waitForWake` and set `wantsToDisplay` to true or false respectively. If the callback
115+
// never calls `display` or `preventDefault(true)`, we will timeout and never update `wantsToDisplay`.
116+
wantsToDisplay = notification.displayWaiter.waitForWake()
119117
}
120118
}.join()
121119
}

0 commit comments

Comments
 (0)