Skip to content

Commit

Permalink
improvement(CheckMonitoredTrip): Add push template and limit notif me…
Browse files Browse the repository at this point in the history
…ssage length.
  • Loading branch information
binh-dam-ibigroup committed Nov 3, 2023
1 parent 114e965 commit e0bb3de
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public class MonitoredTrip extends Model {
/**
* Whether to notify the user when the monitoring of this trip starts.
*/
public boolean notifyAtLeadingInterval;
public boolean notifyAtLeadingInterval = true;

public MonitoredTrip() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public static TripMonitorNotification createItineraryNotFoundNotification(
/**
* Creates an initial reminder of the itinerary monitoring.
*/
public static TripMonitorNotification createInitialReminderNotification() {
public static TripMonitorNotification createInitialReminderNotification(MonitoredTrip trip) {
TripMonitorNotification notification = new TripMonitorNotification();
notification.type = NotificationType.INITIAL_REMINDER;
// TODO: i18n and add itinerary details.
notification.body = "This is a reminder of your upcoming itinerary";
notification.body = String.format("Reminder for your upcoming trip at %s. We will let you know if anything changes.", trip.tripTime);
return notification;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void addInitialReminderIfNeeded() {

if (!trip.isInactive() && isFirstTimeCheckWithinLeadMonitoringTime && userWantsInitialReminder) {
enqueueNotification(
TripMonitorNotification.createInitialReminderNotification()
TripMonitorNotification.createInitialReminderNotification(trip)
);
}
}
Expand Down Expand Up @@ -428,6 +428,7 @@ private void sendNotifications() {
}
Map<String, Object> templateData = Map.of(
"tripId", trip.id,
"tripName", trip.tripName,
"notifications", notifications.stream()
.map(notification -> notification.body)
.collect(Collectors.toList())
Expand Down Expand Up @@ -465,7 +466,7 @@ private boolean sendSMS(OtpUser otpUser, Map<String, Object> data) {
* Send push notification.
*/
private boolean sendPush(OtpUser otpUser, Map<String, Object> data) {
return NotificationUtils.sendPush(otpUser, "MonitoredTripText.ftl", data) != null;
return NotificationUtils.sendPush(otpUser, "MonitoredTripPush.ftl", data) != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import org.opentripplanner.middleware.bugsnag.BugsnagReporter;
import org.opentripplanner.middleware.models.AdminUser;
import org.opentripplanner.middleware.models.OtpUser;
import org.opentripplanner.middleware.utils.HttpUtils;
import org.opentripplanner.middleware.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -44,6 +42,9 @@ public class NotificationUtils {
private static final String PUSH_API_KEY = getConfigPropertyAsText("PUSH_API_KEY");
private static final String PUSH_API_URL = getConfigPropertyAsText("PUSH_API_URL");

/** Lowest permitted push message length between Android and iOS. */
private static final int PUSH_MESSAGE_MAX_LENGTH = 178;

/**
* @param otpUser target user
* @param textTemplate template to use for email in text format
Expand Down Expand Up @@ -71,8 +72,13 @@ public static String sendPush(OtpUser otpUser, String textTemplate, Object templ
*/
static String sendPush(String toUser, String body) {
try {
var jsonBody = "{\"user\":\"" + toUser + "\",\"message\":\"" + body + "\"}";
Map<String, String> headers = Map.of("Accept", "application/json");
// Trim message length (iOS limitation) and escape carriage returns.
var jsonBody = "{\"user\":\"" + toUser + "\",\"message\":\"" + body.substring(0, PUSH_MESSAGE_MAX_LENGTH - 1) + "\"}";
jsonBody = jsonBody.replace("\n", "\\n");
Map<String, String> headers = Map.of(
"Accept", "application/json",
"Content-Type", "application/json"
);
var httpResponse = HttpUtils.httpRequestRawResponse(
URI.create(PUSH_API_URL + "/notification/publish?api_key=" + PUSH_API_KEY),
1000,
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/templates/MonitoredTripPush.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<#--
This is a template for push notifications content for notifications about an
OTP user's monitored trip.
Note the following character limitations by mobile OS:
- iOS: 178 characters over up to 4 lines,
- Android: 240 characters (We are not using notification title at this time).
The max length is thus 178 characters.
-->${tripName}
<#list notifications as notification>
${notification}
</#list>

0 comments on commit e0bb3de

Please sign in to comment.