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