diff --git a/android-sdk/src/main/java/com/blueshift/inappmessage/InAppManager.java b/android-sdk/src/main/java/com/blueshift/inappmessage/InAppManager.java index b3544de0..423fa167 100644 --- a/android-sdk/src/main/java/com/blueshift/inappmessage/InAppManager.java +++ b/android-sdk/src/main/java/com/blueshift/inappmessage/InAppManager.java @@ -157,6 +157,15 @@ public static void unregisterForInAppMessages(Activity activity) { displayConfig.reset(); } + /** + * Returns the name of the current screen registered for in-app message. Else, null. + * + * @return The name of the current screen registered for in-app message. Else, null. + */ + public static String getRegisteredScreenName() { + return displayConfig.screenName; + } + private static void logScreen(String action) { BlueshiftLogger.d(LOG_TAG, action + " { screen: " + displayConfig.screenName + ", activity: " + activityClassCanonicalName(mActivity) + " }"); } diff --git a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxApiManager.java b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxApiManager.java index 07ca6609..fe8a3843 100644 --- a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxApiManager.java +++ b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxApiManager.java @@ -44,13 +44,13 @@ public static List getMessageStatuses(@NonNull fina try { JSONObject jsonObject = new JSONObject(response.getBody()); JSONArray content = jsonObject.optJSONArray("content"); + List statuses = new ArrayList<>(); if (content != null && content.length() > 0) { - List statuses = new ArrayList<>(); for (int i = 0; i < content.length(); i++) { statuses.add(new BlueshiftInboxMessageStatus(content.getJSONObject(i))); } - return statuses; } + return statuses; } catch (JSONException ignore) { } } else { @@ -96,13 +96,13 @@ public static List getNewMessages(final Context context, try { JSONObject jsonObject = new JSONObject(response.getBody()); JSONArray content = jsonObject.optJSONArray("content"); + List messages = new ArrayList<>(); if (content != null && content.length() > 0) { - List messages = new ArrayList<>(); for (int i = 0; i < content.length(); i++) { messages.add(new BlueshiftInboxMessage(content.getJSONObject(i))); } - return messages; } + return messages; } catch (JSONException ignore) { } } else { diff --git a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxManager.java b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxManager.java index d5630eb8..84556593 100644 --- a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxManager.java +++ b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxManager.java @@ -211,18 +211,22 @@ public static void syncMessages(@NonNull Context context, BlueshiftInboxCallback } /** - * Display a given inbox message to the user. + * Display a given inbox message to the user. Calling this method will report the in-app open + * as user initiated in-app open. * * @param message Valid {@link BlueshiftInboxMessage} object * @noinspection unused */ public static void displayInboxMessage(@NonNull BlueshiftInboxMessage message) { - InAppMessage inAppMessage = InAppMessage.getInstance(message.data); - if (inAppMessage != null) { - InAppManager.displayInAppMessage(inAppMessage); - } else { - BlueshiftLogger.d(TAG, "The given message can not be displayed to the user."); - } + BlueshiftExecutor.getInstance().runOnWorkerThread(() -> { + InAppMessage inAppMessage = InAppMessage.getInstance(message.data); + if (inAppMessage != null) { + inAppMessage.setOpenedBy(InAppMessage.OpenedBy.user); + InAppManager.displayInAppMessage(inAppMessage); + } else { + BlueshiftLogger.d(TAG, "The given message can not be displayed to the user."); + } + }); } /** diff --git a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxMessage.java b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxMessage.java index efedc19d..827755b6 100644 --- a/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxMessage.java +++ b/android-sdk/src/main/java/com/blueshift/inbox/BlueshiftInboxMessage.java @@ -4,18 +4,21 @@ import com.blueshift.framework.BlueshiftBaseSQLiteModel; import com.blueshift.inappmessage.InAppMessage; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; -import java.text.SimpleDateFormat; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; -import java.util.Locale; public class BlueshiftInboxMessage extends BlueshiftBaseSQLiteModel { - private long id; // ID for the local database + public long id; // ID for the local database public String accountId; public String userId; public String messageId; @@ -77,6 +80,124 @@ protected void setId(long id) { this.id = id; } + private static long getOrDefaultLong(HashMap hashMap, String key, long defaultVal) { + if (hashMap != null && hashMap.containsKey(key)) { + Object value = hashMap.get(key); + if (value != null) { + try { + return Long.parseLong(String.valueOf(value)); + } catch (Exception ignore) { + } + } + } + + return defaultVal; + } + + private static String getOrDefaultString(HashMap hashMap, String key, String defaultVal) { + if (hashMap != null && hashMap.containsKey(key)) { + Object value = hashMap.get(key); + if (value != null) { + return String.valueOf(value); + } + } + + return defaultVal; + } + + private String getStringFromJSONObject(JSONObject jsonObject, String key) { + String value = ""; + if (jsonObject != null && jsonObject.has(key)) { + try { + value = jsonObject.getString(key); + } catch (JSONException ignored) { + } + } + + return value; + } + + private static JSONObject mapToJSONObject(HashMap map) { + if (map != null && !map.isEmpty()) { + String json = new Gson().toJson(map); + try { + return new JSONObject(json); + } catch (JSONException ignored) { + } + } + + return new JSONObject(); + } + + private HashMap jsonObjectToMap(JSONObject jsonObject) { + if (jsonObject != null) { + Type type = new TypeToken>() { + }.getType(); + return new Gson().fromJson(jsonObject.toString(), type); + } + + return new HashMap<>(); + } + + + /** + * @noinspection unused + */ + public HashMap toHashMap() { + HashMap hashMap = new HashMap<>(); + hashMap.put("id", id); + hashMap.put("messageId", messageId); + + if (data != null) { + JSONObject inbox = data.optJSONObject("inbox"); + if (inbox != null) { + hashMap.put("title", getStringFromJSONObject(inbox, "title")); + hashMap.put("details", getStringFromJSONObject(inbox, "details")); + hashMap.put("imageUrl", getStringFromJSONObject(inbox, "icon")); + } + + hashMap.put("data", jsonObjectToMap(data)); + } + + if (status != null) { + hashMap.put("status", status.toString()); + } + + if (createdAt != null) { + hashMap.put("createdAt", createdAt.getTime() / 1000); + } + + return hashMap; + } + + /** + * @noinspection unused + */ + public static BlueshiftInboxMessage fromHashMap(HashMap hashMap) { + if (hashMap != null) { + BlueshiftInboxMessage inboxMessage = new BlueshiftInboxMessage(); + + inboxMessage.id = getOrDefaultLong(hashMap, "id", 0); + inboxMessage.messageId = getOrDefaultString(hashMap, "messageId", ""); + + String statusString = getOrDefaultString(hashMap, "status", ""); + inboxMessage.status = Status.fromString(statusString); + + //noinspection unchecked + HashMap data = (HashMap) hashMap.get("data"); + if (data != null) { + inboxMessage.data = mapToJSONObject(data); + } + + long seconds = getOrDefaultLong(hashMap, "createdAt", 0); + inboxMessage.createdAt = new Date(seconds * 1000); + + return inboxMessage; + } + + return null; + } + public enum Status { READ, UNREAD, UNKNOWN;