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

Allow preventDefault to be fired up to two times #2138

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading