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);
}
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);
}
}
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[] {}));
}
}
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));
}
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());
}
Aggregations