From 0f42d93847122b56138a223635d5313105142bda Mon Sep 17 00:00:00 2001 From: jinliu9508 Date: Thu, 18 Jul 2024 01:44:18 -0400 Subject: [PATCH] Adding a new trigger redisplays message only if dynamic trigger exists --- .../internal/InAppMessagesManager.kt | 7 +++++-- .../onesignal/inAppMessages/internal/Trigger.kt | 6 ++++++ .../internal/triggers/ITriggerController.kt | 8 ++++++++ .../internal/triggers/impl/TriggerController.kt | 16 ++++++++++++++-- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt index 1b15470ffd..89113ca372 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt @@ -451,7 +451,7 @@ internal class InAppMessagesManager( * * Make all messages with redisplay available if: * - Already displayed - * - At least one existing Trigger has changed OR a new trigger is added + * - At least one existing Trigger has changed OR a new trigger is added when dynamic trigger exists */ private fun makeRedisplayMessagesAvailableWithTriggers( newTriggersKeys: Collection, @@ -460,7 +460,10 @@ internal class InAppMessagesManager( for (message in messages) { if (!message.isTriggerChanged && redisplayedInAppMessages.contains(message) && - (_triggerController.isTriggerOnMessage(message, newTriggersKeys) || isNewTriggerAdded) + ( + _triggerController.isTriggerOnMessage(message, newTriggersKeys) || + isNewTriggerAdded && _triggerController.messageContainsDynamicTrigger(message) + ) ) { Logging.debug("InAppMessagesManager.makeRedisplayMessagesAvailableWithTriggers: Trigger changed for message: $message") message.isTriggerChanged = true diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/Trigger.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/Trigger.kt index 0c37141d11..e63f4c35ea 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/Trigger.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/Trigger.kt @@ -88,6 +88,12 @@ internal class Trigger(json: JSONObject) { */ val value: Any? + /** + * Trigger not kind of CUSTOM or UNKNOWN is considered a dynamic or time-based trigger + */ + val isDynamicTrigger: Boolean + get() = kind != Trigger.OSTriggerKind.CUSTOM && kind != Trigger.OSTriggerKind.UNKNOWN + init { triggerId = json.getString("id") kind = OSTriggerKind.fromString(json.getString("kind")) diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/ITriggerController.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/ITriggerController.kt index 9bd9d0796e..2da5d9243b 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/ITriggerController.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/ITriggerController.kt @@ -41,4 +41,12 @@ internal interface ITriggerController : IEventNotifier { * @return true if the message only has dynamic triggers, false otherwise. */ fun messageHasOnlyDynamicTriggers(message: InAppMessage): Boolean + + /** + * Determine if the provided message contains at least one dynamic trigger. + * + * @param message The message to check. + * @return true if the message contains at least one dynamic triggers, false otherwise. + */ + fun messageContainsDynamicTrigger(message: InAppMessage): Boolean } diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/impl/TriggerController.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/impl/TriggerController.kt index bb1496ae30..1d4979208e 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/impl/TriggerController.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/triggers/impl/TriggerController.kt @@ -54,7 +54,7 @@ internal class TriggerController( return false } - if (trigger.kind != Trigger.OSTriggerKind.CUSTOM) { + if (trigger.isDynamicTrigger) { return _dynamicTriggerController.dynamicTriggerShouldFire(trigger) } @@ -201,7 +201,7 @@ internal class TriggerController( if (message.triggers == null || message.triggers.isEmpty()) return false for (andConditions in message.triggers) { for (trigger in andConditions) { - if (trigger.kind == Trigger.OSTriggerKind.CUSTOM || trigger.kind == Trigger.OSTriggerKind.UNKNOWN) { + if (!trigger.isDynamicTrigger) { // At least one trigger is not dynamic return false } @@ -210,6 +210,18 @@ internal class TriggerController( return true } + override fun messageContainsDynamicTrigger(message: InAppMessage): Boolean { + if (message.triggers == null || message.triggers.isEmpty()) return false + for (andConditions in message.triggers) { + for (trigger in andConditions) { + if (trigger.isDynamicTrigger) { + return true + } + } + } + return false + } + override fun onModelAdded( model: TriggerModel, tag: String,