Skip to content

Commit

Permalink
Merge pull request #2138 from OneSignal/feat/call_prevent_default_mul…
Browse files Browse the repository at this point in the history
…tiple_times

Allow `preventDefault` to be fired multiple times to allow discarding later
  • Loading branch information
nan-li authored Jul 18, 2024
2 parents ec71f7e + 9b7b91d commit 7491122
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ interface INotificationReceivedEvent {

/**
* Call this to prevent OneSignal from displaying the notification automatically.
* This method can be called up to two times with false and then true, if processing time is needed.
* Typically this is only possible within a short
* time-frame (~30 seconds) after the notification is received on the device.
* @param discard an [preventDefault] set to true to dismiss the notification with no
* possibility of displaying it in the future.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ interface INotificationWillDisplayEvent {

/**
* Call this to prevent OneSignal from displaying the notification automatically.
* This method can be called up to two times with false and then true, if processing time is needed.
* Typically this is only possible within a short
* time-frame (~30 seconds) after the notification is received on the device.
* @param discard an [preventDefault] set to true to dismiss the notification with no
* possibility of displaying it in the future.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.onesignal.notifications.internal
import androidx.core.app.NotificationCompat
import com.onesignal.common.safeJSONObject
import com.onesignal.common.safeString
import com.onesignal.common.threading.Waiter
import com.onesignal.common.threading.WaiterWithValue
import com.onesignal.core.internal.time.ITime
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.notifications.BackgroundImageLayout
Expand All @@ -24,7 +24,11 @@ import org.json.JSONObject
*/
class Notification : IDisplayableMutableNotification {
var notificationExtender: NotificationCompat.Extender? = null
val displayWaiter: Waiter = Waiter()

/**
* Wake with true to display the notification, or false to discard it permanently.
*/
val displayWaiter = WaiterWithValue<Boolean>()

override var groupedNotifications: List<Notification>? = null
override var androidNotificationId = 0
Expand Down Expand Up @@ -251,7 +255,7 @@ class Notification : IDisplayableMutableNotification {
}

override fun display() {
displayWaiter.wake()
displayWaiter.wake(true)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ internal class NotificationReceivedEvent(
}

override fun preventDefault(discard: Boolean) {
Logging.debug("NotificationReceivedEvent.preventDefault()")
Logging.debug("NotificationReceivedEvent.preventDefault($discard)")

// If preventDefault(false) has already been called and it is now called again with
// preventDefault(true), the waiter is fired to discard this notification.
// This is necessary for wrapper SDKs that can call preventDefault(discard) twice.
if (isPreventDefault && discard) {
notification.displayWaiter.wake(false)
}
isPreventDefault = true
this.discard = discard
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ internal class NotificationWillDisplayEvent(
}

override fun preventDefault(discard: Boolean) {
Logging.debug("NotificationWillDisplayEvent.preventDefault()")
Logging.debug("NotificationWillDisplayEvent.preventDefault($discard)")

// If preventDefault(false) has already been called and it is now called again with
// preventDefault(true), the waiter is fired to discard this notification.
// This is necessary for wrapper SDKs that can call preventDefault(discard) twice.
if (isPreventDefault && discard) {
notification.displayWaiter.wake(false)
}
isPreventDefault = true
this.discard = discard
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ internal class NotificationGenerationProcessor(
if (notificationReceivedEvent.discard) {
wantsToDisplay = false
} else if (notificationReceivedEvent.isPreventDefault) {
// wait on display waiter. If the caller calls `display` on the notification,
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback
// never calls `display` we will timeout and never set `wantsToDisplay` to true.
wantsToDisplay = false
notification.displayWaiter.waitForWake()
wantsToDisplay = true
// wait on display waiter. If the caller calls `display` or `preventDefault(true)` on the notification,
// we will exit `waitForWake` and set `wantsToDisplay` to true or false respectively. If the callback
// never calls `display` or `preventDefault(true)`, we will timeout and never update `wantsToDisplay`.
wantsToDisplay = notification.displayWaiter.waitForWake()
}
}.join()
}
Expand Down Expand Up @@ -110,12 +109,11 @@ internal class NotificationGenerationProcessor(
if (notificationWillDisplayEvent.discard) {
wantsToDisplay = false
} else if (notificationWillDisplayEvent.isPreventDefault) {
// wait on display waiter. If the caller calls `display` on the notification,
// we will exit `waitForWake` and set `wantsToDisplay` to true. If the callback
// never calls `display` we will timeout and never set `wantsToDisplay` to true.
wantsToDisplay = false
notification.displayWaiter.waitForWake()
wantsToDisplay = true
// wait on display waiter. If the caller calls `display` or `preventDefault(true)` on the notification,
// we will exit `waitForWake` and set `wantsToDisplay` to true or false respectively. If the callback
// never calls `display` or `preventDefault(true)`, we will timeout and never update `wantsToDisplay`.
wantsToDisplay = notification.displayWaiter.waitForWake()
}
}.join()
}
Expand Down
Loading

0 comments on commit 7491122

Please sign in to comment.