Search in sources :

Example 36 with Message

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

the class GeoReporter method prepareEventReportBody.

/**
 * Creates event report request body based on provided geofencing report.
 *
 * @param geoMessageStore message store for geo
 * @param geoReports      map that contains original signaling messages as keys and related geo reports as values.
 * @return request body for geo reporting.
 */
@NonNull
private static EventReportBody prepareEventReportBody(Context context, MessageStore geoMessageStore, @NonNull GeoReport[] geoReports) {
    Set<MessagePayload> messagePayloads = new HashSet<>();
    Set<EventReport> eventReports = new HashSet<>();
    List<Message> messages = geoMessageStore.findAll(context);
    for (GeoReport r : geoReports) {
        Message m = GeoReportHelper.getSignalingMessageForReport(messages, r);
        if (m == null) {
            MobileMessagingLogger.e("Cannot find signaling message for id: " + r.getSignalingMessageId());
            continue;
        }
        messagePayloads.add(new MessagePayload(m.getMessageId(), m.getTitle(), m.getBody(), m.getSound(), m.isVibrate(), m.getCategory(), m.isSilent(), m.getCustomPayload() != null ? m.getCustomPayload().toString() : null, InternalDataMapper.createInternalDataBasedOnMessageContents(m)));
        Long timestampDelta = Time.now() - r.getTimestampOccurred();
        Long timestampDeltaSeconds = TimeUnit.MILLISECONDS.toSeconds(timestampDelta);
        eventReports.add(new EventReport(EventType.valueOf(r.getEvent().name()), r.getArea().getId(), r.getCampaignId(), r.getSignalingMessageId(), r.getMessageId(), timestampDeltaSeconds));
    }
    String deviceInstanceId = MobileMessagingCore.getInstance(context).getPushRegistrationId();
    return new EventReportBody(messagePayloads, eventReports, deviceInstanceId);
}
Also used : Message(org.infobip.mobile.messaging.Message) EventReport(org.infobip.mobile.messaging.api.geo.EventReport) EventReportBody(org.infobip.mobile.messaging.api.geo.EventReportBody) MessagePayload(org.infobip.mobile.messaging.api.geo.MessagePayload) HashSet(java.util.HashSet) NonNull(android.support.annotation.NonNull)

Example 37 with Message

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

the class GeoAreasHandler method handleTransition.

/**
 * Handles geofencing intent and reports corresponding areas to server
 *
 * @param transition resolved transition information
 */
@SuppressWarnings("WeakerAccess")
public void handleTransition(GeoTransition transition) {
    MobileMessagingLogger.v("GEO TRANSITION", transition);
    Map<Message, List<Area>> messagesAndAreas = GeoReportHelper.findSignalingMessagesAndAreas(context, geoMessageStore, transition.getRequestIds(), transition.getEventType());
    if (messagesAndAreas.isEmpty()) {
        MobileMessagingLogger.d(TAG, "No messages for triggered areas");
        return;
    }
    logGeofences(messagesAndAreas.values(), transition.getEventType());
    geofencingHelper.addUnreportedGeoEvents(GeoReportHelper.createReportsForMultipleMessages(context, messagesAndAreas, transition.getEventType(), transition.getTriggeringLocation()));
    GeoReport[] unreportedEvents = geofencingHelper.removeUnreportedGeoEvents();
    if (unreportedEvents.length == 0) {
        MobileMessagingLogger.d(TAG, "No geofencing events to report at current time");
        return;
    }
    try {
        GeoReportingResult result = geoReporter.reportSync(unreportedEvents);
        handleReportingResultWithNewMessagesAndNotifications(unreportedEvents, result);
    } catch (Exception e) {
        MobileMessagingLogger.e(TAG, "Failed to report geo events " + e.toString());
        handleGeoReportingResult(context, new GeoReportingResult(e));
    }
}
Also used : GeoReport(org.infobip.mobile.messaging.geo.report.GeoReport) Message(org.infobip.mobile.messaging.Message) List(java.util.List) GeoReportingResult(org.infobip.mobile.messaging.geo.report.GeoReportingResult)

Example 38 with Message

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

the class MessagesMapper method mapResponseToMessages.

static List<Message> mapResponseToMessages(List<MessageResponse> payloads) {
    if (payloads == null)
        return Collections.emptyList();
    List<Message> messages = new ArrayList<>(payloads.size());
    for (MessageResponse payload : payloads) {
        if (payload == null) {
            continue;
        }
        Message message = responseToMessage(payload);
        messages.add(message);
    }
    return messages;
}
Also used : Message(org.infobip.mobile.messaging.Message) MessageResponse(org.infobip.mobile.messaging.api.messages.MessageResponse) ArrayList(java.util.ArrayList)

Example 39 with Message

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

the class MessagesSynchronizer method sync.

public void sync() {
    if (!mobileMessagingCore.isPushRegistrationEnabled()) {
        return;
    }
    final String[] unreportedMessageIds = mobileMessagingCore.getAndRemoveUnreportedMessageIds();
    new MRetryableTask<Void, List<Message>>() {

        @Override
        public List<Message> run(Void[] objects) {
            if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
                MobileMessagingLogger.w("Can't sync messages without valid registration");
                throw InternalSdkError.NO_VALID_REGISTRATION.getException();
            }
            String[] messageIds = mobileMessagingCore.getSyncMessagesIds();
            SyncMessagesBody syncMessagesBody = SyncMessagesBody.make(messageIds, unreportedMessageIds);
            MobileMessagingLogger.v("SYNC MESSAGES >>>", syncMessagesBody);
            SyncMessagesResponse syncMessagesResponse = mobileApiMessages.sync(syncMessagesBody);
            MobileMessagingLogger.v("SYNC MESSAGES <<<", syncMessagesResponse);
            return MessagesMapper.mapResponseToMessages(syncMessagesResponse.getPayloads());
        }

        @Override
        public void after(List<Message> messages) {
            broadcaster.deliveryReported(unreportedMessageIds);
            if (messages == null || messages.isEmpty()) {
                return;
            }
            for (Message message : messages) {
                mobileMessageHandler.handleMessage(message);
            }
        }

        @Override
        public void error(Throwable error) {
            mobileMessagingCore.addUnreportedMessageIds(unreportedMessageIds);
            mobileMessagingCore.setLastHttpException(error);
            MobileMessagingLogger.e("MobileMessaging API returned error (synchronizing messages)! ", error);
            stats.reportError(MobileMessagingStatsError.SYNC_MESSAGES_ERROR);
            if (!(error instanceof InternalSdkError.InternalSdkException)) {
                broadcaster.error(MobileMessagingError.createFrom(error));
            }
        }
    }.retryWith(retryPolicy).execute(executor);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) SyncMessagesResponse(org.infobip.mobile.messaging.api.messages.SyncMessagesResponse) Message(org.infobip.mobile.messaging.Message) SyncMessagesBody(org.infobip.mobile.messaging.api.messages.SyncMessagesBody) List(java.util.List)

Example 40 with Message

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

the class MobileMessagingGcmIntentService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    switch(action) {
        case ACTION_GCM_MESSAGE_RECEIVE:
            Message message = FCMMessageMapper.fromCloudBundle(intent.getExtras());
            MobileMessagingLogger.v("RECEIVED MESSAGE FROM FCM", message);
            mobileMessageHandler().handleMessage(message);
            break;
        case ACTION_TOKEN_CLEANUP:
            registrationTokenHandler.handleRegistrationTokenCleanup(this, intent.getStringExtra(EXTRA_GCM_SENDER_ID));
            break;
        case ACTION_ACQUIRE_INSTANCE_ID:
            registrationTokenHandler.handleRegistrationTokenUpdate(this);
            break;
        case ACTION_FCM_MESSAGE_RECEIVE:
            MobileMessagingLogger.v("Received message event from Firebase SDK, ignoring to avoid duplicate notifications");
            break;
    }
}
Also used : Message(org.infobip.mobile.messaging.Message)

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