Search in sources :

Example 1 with JsonSerializer

use of org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer in project mobile-messaging-sdk-android by infobip.

the class DatabaseHelperImpl method setSendDateTimeToReceivedTImeIfAbsent.

private void setSendDateTimeToReceivedTImeIfAbsent(SQLiteDatabase db) {
    // Read existing data from database
    class Message {

        private String id;

        private String title;

        private String body;

        private String sound;

        private int vibrate;

        private String icon;

        private short silent;

        private String category;

        private String from;

        private long receivedTimestamp;

        private long seenTimestamp;

        private String customPayload;

        private String internalData;

        private String contentUrl;

        private String destination;

        private String status;

        private String statusMessage;
    }
    Cursor cursor = db.rawQuery("SELECT * FROM messages", new String[0]);
    List<Message> messages = new ArrayList<>();
    if (cursor.moveToFirst()) {
        do {
            try {
                Message message = new Message();
                message.id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
                message.title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
                message.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                message.sound = cursor.getString(cursor.getColumnIndexOrThrow("sound"));
                message.vibrate = cursor.getInt(cursor.getColumnIndexOrThrow("vibrate"));
                message.icon = cursor.getString(cursor.getColumnIndexOrThrow("icon"));
                message.silent = cursor.getShort(cursor.getColumnIndexOrThrow("silent"));
                message.category = cursor.getString(cursor.getColumnIndexOrThrow("category"));
                message.from = cursor.getString(cursor.getColumnIndexOrThrow("_from"));
                message.receivedTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow("received_timestamp"));
                message.seenTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow("seen_timestamp"));
                message.customPayload = cursor.getString(cursor.getColumnIndexOrThrow("custom_payload"));
                message.internalData = cursor.getString(cursor.getColumnIndexOrThrow("internal_data"));
                message.contentUrl = cursor.getString(cursor.getColumnIndexOrThrow("content_url"));
                message.destination = cursor.getString(cursor.getColumnIndexOrThrow("destination"));
                message.status = cursor.getString(cursor.getColumnIndexOrThrow("status"));
                message.statusMessage = cursor.getString(cursor.getColumnIndexOrThrow("status_message"));
                messages.add(message);
            } catch (Exception e) {
                MobileMessagingLogger.e(Log.getStackTraceString(e));
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    JsonSerializer serializer = new JsonSerializer(false);
    for (Message m : messages) {
        // Set sendDateTime in internalData if absent
        Map internalDataMap = serializer.deserialize(m.internalData, HashMap.class);
        if (internalDataMap == null) {
            internalDataMap = new HashMap();
        }
        if (!internalDataMap.containsKey("sendDateTime")) {
            // noinspection unchecked
            internalDataMap.put("sendDateTime", m.receivedTimestamp);
        }
        m.internalData = serializer.serialize(internalDataMap);
        // Save new data to database
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", m.id);
        contentValues.put("title", m.title);
        contentValues.put("body", m.body);
        contentValues.put("sound", m.sound);
        contentValues.put("vibrate", m.vibrate);
        contentValues.put("icon", m.icon);
        contentValues.put("silent", m.silent);
        contentValues.put("category", m.category);
        contentValues.put("_from", m.from);
        contentValues.put("received_timestamp", m.receivedTimestamp);
        contentValues.put("seen_timestamp", m.seenTimestamp);
        contentValues.put("custom_payload", m.customPayload);
        contentValues.put("internal_data", m.internalData);
        contentValues.put("content_url", m.contentUrl);
        contentValues.put("destination", m.destination);
        contentValues.put("status", m.status);
        contentValues.put("status_message", m.statusMessage);
        db.insertWithOnConflict("messages", null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    }
}
Also used : ContentValues(android.content.ContentValues) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonSerializer(org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer) Cursor(android.database.Cursor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with JsonSerializer

use of org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer in project mobile-messaging-sdk-android by infobip.

the class GeoEventsTest method test_handle_event_notification_settings.

@Test
public void test_handle_event_notification_settings() throws InterruptedException {
    GeoTest geo = new JsonSerializer().deserialize(internalData(), GeoTest.class);
    int entryCnt = 0;
    for (int i = 0; i < 5; i++) {
        List<GeoEventSettings> events = geo.getEventFilters();
        for (GeoEventSettings event : events) {
            for (Area area : geo.getAreasList()) {
                // time travel - simulate delay between events
                advanceTimeByMS(TimeUnit.MINUTES.toMillis(event.getTimeoutInMinutes() + 1));
                long lastDisplayTime = getLastNotificationTimeForArea(area);
                int timesTriggered = getNumberOfDisplayedNotificationsForArea(area);
                long timeIntervalBetweenEvents = currentTimeMillis() - lastDisplayTime;
                boolean isTimeoutExpired = timeIntervalBetweenEvents > TimeUnit.MINUTES.toMillis(event.getTimeoutInMinutes());
                int eventLimit = event.getLimit();
                boolean isLimitBreached = eventLimit != GeoEventSettings.UNLIMITED_RECURRING && timesTriggered >= eventLimit;
                if (isTimeoutExpired && !isLimitBreached) {
                    if (eventLimit != GeoEventSettings.UNLIMITED_RECURRING) {
                        ++timesTriggered;
                    }
                    lastDisplayTime = currentTimeMillis();
                    setLastNotificationTimeForArea(area, lastDisplayTime);
                    setNumberOfDisplayedNotificationsForArea(area, timesTriggered);
                    entryCnt++;
                }
            }
        }
    }
    assertEquals(entryCnt, 5);
}
Also used : JsonSerializer(org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer) Test(org.junit.Test)

Example 3 with JsonSerializer

use of org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer in project mobile-messaging-sdk-android by infobip.

the class MobileMessagingTestCase method createMessage.

/**
 * Generates messages with provided ids and geo campaign object
 *
 * @param saveToStorage set to true to save messages to message store
 * @param messageId     message id for a message
 * @param geo           geo campaign object
 * @return new message
 */
protected static Message createMessage(Context context, String messageId, boolean saveToStorage, Geo... geo) {
    Message message = new Message();
    message.setMessageId(messageId);
    message.setSentTimestamp(0);
    boolean isGeo = geo.length > 0 && geo[0] != null && geo[0].getAreasList() != null && !geo[0].getAreasList().isEmpty();
    if (isGeo) {
        JsonSerializer serializer = new JsonSerializer();
        message.setInternalData(GeoDataMapper.geoToInternalData(geo[0]));
        message.setContentUrl(InternalDataMapper.getInternalDataContentUrl(serializer.serialize(geo[0])));
    }
    if (saveToStorage) {
        geoStore.save(context, message);
    }
    return message;
}
Also used : Message(org.infobip.mobile.messaging.Message) JsonSerializer(org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer)

Example 4 with JsonSerializer

use of org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer in project mobile-messaging-sdk-android by infobip.

the class FCMMessageTest method test_geofence_expiryTime.

public void test_geofence_expiryTime() throws Exception {
    String geofence = "{" + "\"expiryTime\":\"2016-08-06T12:20:16+03:00\"" + "}";
    GeoEventsTest.GeoTest geo = new JsonSerializer().deserialize(geofence, GeoEventsTest.GeoTest.class);
    assertNotNull(geo.getExpiryDate());
    assertNotEquals(0L, geo.getExpiryDate().getTime());
    geofence = "{" + "\"expiryTime\":\"2016-12-06T13:20:16+0300\"" + "}";
    geo = new JsonSerializer().deserialize(geofence, GeoEventsTest.GeoTest.class);
    assertNotNull(geo.getExpiryDate());
    assertNotEquals(0L, geo.getExpiryDate().getTime());
    geofence = "{" + "\"expiryTime\":\"2016-08-31T14:20:16+03\"" + "}";
    geo = new JsonSerializer().deserialize(geofence, GeoEventsTest.GeoTest.class);
    assertNotNull(geo.getExpiryDate());
    assertNotEquals(0L, geo.getExpiryDate().getTime());
    geofence = "{" + "\"expiryTime\":\"2016-08-31T14:20:16Z\"" + "}";
    geo = new JsonSerializer().deserialize(geofence, GeoEventsTest.GeoTest.class);
    assertNotNull(geo.getExpiryDate());
    assertNotEquals(0L, geo.getExpiryDate().getTime());
}
Also used : JsonSerializer(org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer)

Aggregations

JsonSerializer (org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer)4 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Message (org.infobip.mobile.messaging.Message)1 Test (org.junit.Test)1