Search in sources :

Example 1 with Geo

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

the class GeoReportHelper method findSignalingMessagesAndAreas.

/**
 * Returns map of signaling messages and corresponding areas that match geofence and transition event
 *
 * @param messageStore message store to look messages for
 * @param requestIds   requestIds received from Google Location Services during transition event
 * @param event        transition event type
 * @return signaling messages with corresponding areas
 */
@NonNull
public static Map<Message, List<Area>> findSignalingMessagesAndAreas(Context context, MessageStore messageStore, Set<String> requestIds, @NonNull GeoEventType event) {
    Date now = Time.date();
    Map<Message, List<Area>> messagesAndAreas = new ArrayMap<>();
    for (Message message : messageStore.findAll(context)) {
        Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
        if (geo == null || geo.getAreasList() == null || geo.getAreasList().isEmpty()) {
            continue;
        }
        // don't trigger geo event before start date
        Date startDate = geo.getStartDate();
        if (startDate != null && startDate.after(now)) {
            continue;
        }
        List<Area> campaignAreas = geo.getAreasList();
        List<Area> triggeredAreas = new ArrayList<>();
        for (Area area : campaignAreas) {
            for (String requestId : requestIds) {
                if (!requestId.equalsIgnoreCase(area.getId())) {
                    continue;
                }
                if (!GeoNotificationHelper.shouldReportTransition(context, geo, event)) {
                    continue;
                }
                triggeredAreas.add(area);
            }
        }
        if (!triggeredAreas.isEmpty()) {
            messagesAndAreas.put(message, triggeredAreas);
        }
    }
    return filterOverlappingAreas(messagesAndAreas);
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) ArrayList(java.util.ArrayList) ArrayMap(android.support.v4.util.ArrayMap) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) NonNull(android.support.annotation.NonNull)

Example 2 with Geo

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

the class GeoNotificationHelper method notifyAboutGeoTransitions.

/**
 * Broadcasts geofencing events and displays appropriate notifications for geo events
 *
 * @param messages messages with geo to notify
 */
public void notifyAboutGeoTransitions(Map<Message, GeoEventType> messages) {
    for (Message m : messages.keySet()) {
        GeoEventType eventType = messages.get(m);
        Geo geo = GeoDataMapper.geoFromInternalData(m.getInternalData());
        if (geo == null)
            continue;
        setLastNotificationTimeForArea(context, geo.getCampaignId(), eventType, Time.now());
        setNumberOfDisplayedNotificationsForArea(context, geo.getCampaignId(), eventType, getNumberOfDisplayedNotificationsForArea(context, geo.getCampaignId(), eventType) + 1);
        notifyAboutTransition(geo, m, eventType);
    }
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) GeoMessage(org.infobip.mobile.messaging.geo.GeoMessage) Message(org.infobip.mobile.messaging.Message) GeoEventType(org.infobip.mobile.messaging.geo.GeoEventType)

Example 3 with Geo

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

the class GeofencingImpl method removeExpiredAreasFromStorage.

void removeExpiredAreasFromStorage() {
    GeoSQLiteMessageStore messageStoreForGeo = (GeoSQLiteMessageStore) geofencingHelper.getMessageStoreForGeo();
    List<Message> messages = messageStoreForGeo.findAll(context);
    List<String> messageIdsToDelete = new ArrayList<>(messages.size());
    Date now = new Date();
    for (Message message : messages) {
        Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
        if (geo == null) {
            continue;
        }
        List<Area> areasList = geo.getAreasList();
        Date expiryDate = geo.getExpiryDate();
        if (areasList == null || areasList.isEmpty()) {
            continue;
        }
        for (Area area : areasList) {
            if (!area.isValid() || expiryDate == null) {
                continue;
            }
            if (expiryDate.before(now)) {
                messageIdsToDelete.add(message.getMessageId());
            }
        }
    }
    if (!messageIdsToDelete.isEmpty()) {
        messageStoreForGeo.deleteByIds(context, messageIdsToDelete.toArray(new String[] {}));
    }
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) ArrayList(java.util.ArrayList) GeoSQLiteMessageStore(org.infobip.mobile.messaging.geo.storage.GeoSQLiteMessageStore) Date(java.util.Date)

Example 4 with Geo

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

the class GeofencingImpl method calculateGeofencesToMonitorAndNextCheckDates.

@SuppressWarnings("WeakerAccess")
private Pair<List<Geofence>, Pair<Date, Date>> calculateGeofencesToMonitorAndNextCheckDates(MessageStore messageStore) {
    Date nextCheckRefreshDate = null;
    Date nextCheckExpireDate = null;
    Map<String, Geofence> geofences = new HashMap<>();
    Map<String, Date> expiryDates = new HashMap<>();
    List<Message> messages = messageStore.findAll(context);
    for (Message message : messages) {
        Geo geo = GeoDataMapper.geoFromInternalData(message.getInternalData());
        if (geo == null || geo.getAreasList() == null || geo.getAreasList().isEmpty()) {
            continue;
        }
        nextCheckExpireDate = calculateNextCheckDateForGeoExpiry(geo, nextCheckExpireDate);
        final Set<String> finishedCampaignIds = GeofencingHelper.getFinishedCampaignIds(context);
        if (finishedCampaignIds.contains(geo.getCampaignId())) {
            continue;
        }
        if (geo.isEligibleForMonitoring()) {
            List<Area> geoAreasList = geo.getAreasList();
            for (Area area : geoAreasList) {
                if (!area.isValid()) {
                    continue;
                }
                Date expiry = expiryDates.get(area.getId());
                if (expiry != null && expiry.after(geo.getExpiryDate())) {
                    continue;
                }
                expiryDates.put(area.getId(), geo.getExpiryDate());
                geofences.put(area.getId(), area.toGeofence(geo.getExpiryDate()));
            }
        }
        nextCheckRefreshDate = calculateNextCheckDateForGeoStart(geo, nextCheckRefreshDate);
    }
    List<Geofence> geofenceList = new ArrayList<>(geofences.values());
    return new Pair<>(geofenceList, new Pair<>(nextCheckRefreshDate, nextCheckExpireDate));
}
Also used : Message(org.infobip.mobile.messaging.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Geofence(com.google.android.gms.location.Geofence) Pair(android.util.Pair)

Example 5 with Geo

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

the class GeoReportHelper method createNewMessageForReport.

/**
 * Creates new message based on geofencing report
 *
 * @param report          geofencing report for any supported event
 * @param reportingResult result of reporting geo events to server
 * @param originalMessage original signaling message
 * @return new message based on triggering event, area and original signaling message
 */
private static Message createNewMessageForReport(@NonNull final GeoReport report, @NonNull GeoReportingResult reportingResult, @NonNull Message originalMessage) {
    GeoLatLng triggeringLocation = report.getTriggeringLocation();
    if (triggeringLocation == null) {
        triggeringLocation = new GeoLatLng(null, null);
    }
    List<Area> areas = new ArrayList<>();
    if (report.getArea() != null) {
        areas.add(report.getArea());
    }
    Geo geo;
    Geo originalMessageGeo = GeoDataMapper.geoFromInternalData(originalMessage.getInternalData());
    if (originalMessageGeo != null) {
        geo = new Geo(triggeringLocation.getLat(), triggeringLocation.getLng(), originalMessageGeo.getDeliveryTime(), originalMessageGeo.getExpiryTime(), originalMessageGeo.getStartTime(), originalMessageGeo.getCampaignId(), areas, originalMessageGeo.getEvents(), originalMessage.getSentTimestamp(), originalMessage.getContentUrl());
    } else {
        geo = new Geo(triggeringLocation.getLat(), triggeringLocation.getLng(), null, null, null, null, areas, null, Time.now(), originalMessage.getContentUrl());
    }
    String internalData = GeoDataMapper.geoToInternalData(geo);
    return new Message(getMessageIdFromReport(report, reportingResult), originalMessage.getTitle(), originalMessage.getBody(), originalMessage.getSound(), originalMessage.isVibrate(), originalMessage.getIcon(), // enforcing non-silent
    false, originalMessage.getCategory(), originalMessage.getFrom(), Time.now(), 0, Time.now(), originalMessage.getCustomPayload(), internalData, originalMessage.getDestination(), originalMessage.getStatus(), originalMessage.getStatusMessage(), originalMessage.getContentUrl());
}
Also used : Geo(org.infobip.mobile.messaging.geo.Geo) Area(org.infobip.mobile.messaging.geo.Area) Message(org.infobip.mobile.messaging.Message) GeoLatLng(org.infobip.mobile.messaging.geo.GeoLatLng) ArrayList(java.util.ArrayList)

Aggregations

Message (org.infobip.mobile.messaging.Message)6 Geo (org.infobip.mobile.messaging.geo.Geo)6 ArrayList (java.util.ArrayList)4 Area (org.infobip.mobile.messaging.geo.Area)4 Date (java.util.Date)3 GeoMessage (org.infobip.mobile.messaging.geo.GeoMessage)2 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 ArrayMap (android.support.v4.util.ArrayMap)1 Pair (android.util.Pair)1 Geofence (com.google.android.gms.location.Geofence)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GeoEventSettings (org.infobip.mobile.messaging.geo.GeoEventSettings)1 GeoEventType (org.infobip.mobile.messaging.geo.GeoEventType)1 GeoLatLng (org.infobip.mobile.messaging.geo.GeoLatLng)1 GeoSQLiteMessageStore (org.infobip.mobile.messaging.geo.storage.GeoSQLiteMessageStore)1 Test (org.junit.Test)1