Skip to content

Commit 20c7f2a

Browse files
committed
Add OSInAppMessagePreviewHandler kotlin file
* Refactor from Java to Kotlin
1 parent e280c0a commit 20c7f2a

File tree

2 files changed

+47
-57
lines changed

2 files changed

+47
-57
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessagePreviewHandler.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.onesignal
2+
3+
import android.content.Context
4+
import android.os.Build
5+
import android.os.Bundle
6+
import org.json.JSONException
7+
import org.json.JSONObject
8+
9+
internal object OSInAppMessagePreviewHandler {
10+
@JvmStatic
11+
fun inAppMessagePreviewHandled(context: Context?, bundle: Bundle?): Boolean {
12+
val pushPayloadJson = NotificationBundleProcessor.bundleAsJSONObject(bundle)
13+
// Show In-App message preview it is in the payload & the app is in focus
14+
val previewUUID = inAppPreviewPushUUID(pushPayloadJson) ?: return false
15+
16+
// If app is in focus display the IAMs preview now
17+
if (OneSignal.isAppActive()) {
18+
OneSignal.getInAppMessageController().displayPreviewMessage(previewUUID)
19+
} else if (shouldDisplayNotification()) {
20+
val generationJob = OSNotificationGenerationJob(context, pushPayloadJson)
21+
GenerateNotification.displayIAMPreviewNotification(generationJob)
22+
}
23+
return true
24+
}
25+
26+
@JvmStatic
27+
fun inAppPreviewPushUUID(payload: JSONObject): String? {
28+
val osCustom: JSONObject = try {
29+
NotificationBundleProcessor.getCustomJSONObject(payload)
30+
} catch (e: JSONException) {
31+
return null
32+
}
33+
34+
if (!osCustom.has(NotificationBundleProcessor.PUSH_ADDITIONAL_DATA_KEY))
35+
return null
36+
37+
return osCustom.optJSONObject(NotificationBundleProcessor.PUSH_ADDITIONAL_DATA_KEY)?.let { additionalData ->
38+
if (additionalData.has(NotificationBundleProcessor.IAM_PREVIEW_KEY))
39+
additionalData.optString(NotificationBundleProcessor.IAM_PREVIEW_KEY)
40+
else
41+
null
42+
}
43+
}
44+
45+
// Validate that the current Android device is Android 4.4 or higher
46+
private fun shouldDisplayNotification(): Boolean = Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2
47+
}

0 commit comments

Comments
 (0)