Skip to content

Commit

Permalink
[MOBL-1590] Mobile Inbox helper methods for the plugins to consume (#335
Browse files Browse the repository at this point in the history
)

* return empty list if the response is empty instead of returning null

* set openedBy as user when the host app calls the displayInboxMessage to show in-app message

* added helper methods for data transfer between native sdk and flutter plugin

* added helper method to transform the message object to map and back for plugins to use

* move the in-app display to background thread for improved performance

* added a method to get the current screen name registered for in-app messages
  • Loading branch information
rahulrvp authored Jun 6, 2023
1 parent eec395c commit 5992227
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) + " }");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public static List<BlueshiftInboxMessageStatus> getMessageStatuses(@NonNull fina
try {
JSONObject jsonObject = new JSONObject(response.getBody());
JSONArray content = jsonObject.optJSONArray("content");
List<BlueshiftInboxMessageStatus> statuses = new ArrayList<>();
if (content != null && content.length() > 0) {
List<BlueshiftInboxMessageStatus> 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 {
Expand Down Expand Up @@ -96,13 +96,13 @@ public static List<BlueshiftInboxMessage> getNewMessages(final Context context,
try {
JSONObject jsonObject = new JSONObject(response.getBody());
JSONArray content = jsonObject.optJSONArray("content");
List<BlueshiftInboxMessage> messages = new ArrayList<>();
if (content != null && content.length() > 0) {
List<BlueshiftInboxMessage> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +80,124 @@ protected void setId(long id) {
this.id = id;
}

private static long getOrDefaultLong(HashMap<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> jsonObjectToMap(JSONObject jsonObject) {
if (jsonObject != null) {
Type type = new TypeToken<HashMap<String, Object>>() {
}.getType();
return new Gson().fromJson(jsonObject.toString(), type);
}

return new HashMap<>();
}


/**
* @noinspection unused
*/
public HashMap<String, Object> toHashMap() {
HashMap<String, Object> 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<String, Object> 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<String, Object> data = (HashMap<String, Object>) 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;

Expand Down

0 comments on commit 5992227

Please sign in to comment.