Search in sources :

Example 61 with Message

use of org.infobip.mobile.messaging.Message in project mobile-messaging-sdk-android by infobip.

the class FCMMessageMapper method fromCloudBundle.

/**
 * De-serializes Push message from the Bundle we receive from GCM/FCM
 *
 * @param bundle data from the intent
 * @return deserialized message
 */
public static Message fromCloudBundle(Bundle bundle) {
    if (bundle == null) {
        return null;
    }
    boolean silent = "true".equals(bundle.getString(BundleField.SILENT.getKey()));
    String messageId = bundle.getString(BundleField.MESSAGE_ID.getKey());
    String icon = bundle.getString(BundleField.ICON.getKey());
    String from = bundle.getString(BundleField.FROM.getKey());
    long receivedTs = bundle.getLong(BundleField.RECEIVED_TIMESTAMP.getKey());
    long seenTs = bundle.getLong(BundleField.SEEN_TIMESTAMP.getKey());
    JSONObject customPayload = getJSON(bundle, BundleField.CUSTOM_PAYLOAD.getKey());
    String internalDataJson = bundle.getString(BundleField.INTERNAL_DATA.getKey());
    boolean vibrate = silent ? InternalDataMapper.getInternalDataVibrate(internalDataJson, true) : "true".equals(bundle.getString(BundleField.VIBRATE.getKey(), "true"));
    String title = silent ? InternalDataMapper.getInternalDataTitle(internalDataJson) : bundle.getString(BundleField.TITLE.getKey());
    String body = silent ? InternalDataMapper.getInternalDataBody(internalDataJson) : bundle.getString(BundleField.BODY.getKey());
    String sound = silent ? InternalDataMapper.getInternalDataSound(internalDataJson) : bundle.getString(BundleField.SOUND2.getKey(), bundle.getString(BundleField.SOUND.getKey()));
    String category = silent ? InternalDataMapper.getInternalDataCategory(internalDataJson) : bundle.getString(BundleField.CATEGORY.getKey());
    String contentUrl = InternalDataMapper.getInternalDataContentUrl(internalDataJson);
    long sentDateTime = InternalDataMapper.getInternalDataSendDateTime(internalDataJson);
    String destination = bundle.getString(BundleField.DESTINATION.getKey());
    String statusMessage = bundle.getString(BundleField.STATUS_MESSAGE.getKey());
    Message.Status status = Message.Status.UNKNOWN;
    try {
        status = Message.Status.valueOf(bundle.getString(BundleField.STATUS.getKey()));
    } catch (Exception ignored) {
    }
    return new Message(messageId, title, body, sound, vibrate, icon, silent, category, from, receivedTs, seenTs, sentDateTime, customPayload, internalDataJson, destination, status, statusMessage, contentUrl);
}
Also used : JSONObject(org.json.JSONObject) Message(org.infobip.mobile.messaging.Message) JSONException(org.json.JSONException)

Example 62 with Message

use of org.infobip.mobile.messaging.Message in project mobile-messaging-sdk-android by infobip.

the class SharedPreferencesMigrator method migrateMessages.

static void migrateMessages(Context context, SQLiteDatabase db) {
    SharedPreferencesMessageStore sharedPreferencesMessageStore = new SharedPreferencesMessageStore();
    List<Message> messages = sharedPreferencesMessageStore.findAll(context);
    if (messages.isEmpty()) {
        return;
    }
    for (Message message : messages) {
        db.insert(SqliteMessage.getTable(), null, SqliteMessage.save(message));
    }
}
Also used : SharedPreferencesMessageStore(org.infobip.mobile.messaging.storage.SharedPreferencesMessageStore) Message(org.infobip.mobile.messaging.Message)

Example 63 with Message

use of org.infobip.mobile.messaging.Message in project mobile-messaging-sdk-android by infobip.

the class MessagesMapper method responseToMessage.

private static Message responseToMessage(MessageResponse response) {
    JSONObject customPayload = null;
    try {
        customPayload = response.getCustomPayload() != null ? new JSONObject(response.getCustomPayload()) : null;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Message message = new Message(response.getMessageId(), response.getTitle(), response.getBody(), response.getSound(), !"false".equals(response.getVibrate()), null, "true".equals(response.getSilent()), response.getCategory(), null, Time.now(), 0, InternalDataMapper.getInternalDataSendDateTime(response.getInternalData()), customPayload, response.getInternalData(), null, Message.Status.UNKNOWN, null, InternalDataMapper.getInternalDataContentUrl(response.getInternalData()));
    InternalDataMapper.updateMessageWithInternalData(message, response.getInternalData());
    return message;
}
Also used : JSONObject(org.json.JSONObject) Message(org.infobip.mobile.messaging.Message) JSONException(org.json.JSONException)

Example 64 with Message

use of org.infobip.mobile.messaging.Message in project mobile-messaging-sdk-android by infobip.

the class MoMessageMapper method body.

static MoMessagesBody body(String pushRegistrationId, Message[] messages) {
    List<MoMessage> moMessages = new ArrayList<>();
    for (Message message : messages) {
        String customPayloadString = message.getCustomPayload() != null ? message.getCustomPayload().toString() : null;
        Map customPayloadMap = serializer.deserialize(customPayloadString, Map.class);
        String internalData = message.getInternalData();
        moMessages.add(new MoMessage(message.getMessageId(), message.getDestination(), message.getBody(), InternalDataMapper.getInternalDataInitialMessageId(internalData), InternalDataMapper.getInternalDataBulkId(internalData), customPayloadMap));
    }
    MoMessagesBody moMessagesBody = new MoMessagesBody();
    moMessagesBody.setFrom(pushRegistrationId);
    moMessagesBody.setMessages(moMessages.toArray(new MoMessage[moMessages.size()]));
    return moMessagesBody;
}
Also used : MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage) Message(org.infobip.mobile.messaging.Message) ArrayList(java.util.ArrayList) MoMessagesBody(org.infobip.mobile.messaging.api.messages.MoMessagesBody) Map(java.util.Map) MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage)

Example 65 with Message

use of org.infobip.mobile.messaging.Message in project mobile-messaging-sdk-android by infobip.

the class MoMessageMapper method messages.

static Message[] messages(MoMessagesResponse response) {
    if (response == null || response.getMessages() == null || response.getMessages().length == 0) {
        return new Message[0];
    }
    List<Message> messages = new ArrayList<>(response.getMessages().length);
    for (MoMessageDelivery delivery : response.getMessages()) {
        Message message = new Message();
        message.setMessageId(delivery.getMessageId());
        message.setDestination(delivery.getDestination());
        message.setBody(delivery.getText());
        message.setStatusMessage(delivery.getStatus());
        message.setReceivedTimestamp(Time.now());
        message.setSeenTimestamp(Time.now());
        message.setCustomPayload(delivery.getCustomPayload() != null ? new JSONObject(delivery.getCustomPayload()) : null);
        Message.Status status = Message.Status.UNKNOWN;
        int statusCode = delivery.getStatusCode();
        if (statusCode < Message.Status.values().length) {
            status = Message.Status.values()[statusCode];
        } else {
            MobileMessagingLogger.e(delivery.getMessageId() + ":Unexpected status code: " + statusCode);
        }
        message.setStatus(status);
        messages.add(message);
    }
    return messages.toArray(new Message[messages.size()]);
}
Also used : MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage) Message(org.infobip.mobile.messaging.Message) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) MoMessageDelivery(org.infobip.mobile.messaging.api.messages.MoMessageDelivery)

Aggregations

Message (org.infobip.mobile.messaging.Message)87 Test (org.junit.Test)44 Intent (android.content.Intent)13 Bundle (android.os.Bundle)13 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 GeoReportingResult (org.infobip.mobile.messaging.geo.report.GeoReportingResult)10 Context (android.content.Context)7 GeoReport (org.infobip.mobile.messaging.geo.report.GeoReport)7 GeoTransition (org.infobip.mobile.messaging.geo.transition.GeoTransition)7 JSONObject (org.json.JSONObject)7 List (java.util.List)6 EventReportResponse (org.infobip.mobile.messaging.api.geo.EventReportResponse)6 Geo (org.infobip.mobile.messaging.geo.Geo)6 Map (java.util.Map)5 MoMessage (org.infobip.mobile.messaging.api.messages.MoMessage)5 Area (org.infobip.mobile.messaging.geo.Area)5 NotificationAction (org.infobip.mobile.messaging.interactive.NotificationAction)5 NotificationCategory (org.infobip.mobile.messaging.interactive.NotificationCategory)5 Date (java.util.Date)4