From e0bb3de63b39d698bbf7944f1a94ec231cc66580 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:40:29 -0400 Subject: [PATCH] improvement(CheckMonitoredTrip): Add push template and limit notif message length. --- .../middleware/models/MonitoredTrip.java | 2 +- .../middleware/models/TripMonitorNotification.java | 4 ++-- .../tripmonitor/jobs/CheckMonitoredTrip.java | 5 +++-- .../middleware/utils/NotificationUtils.java | 14 ++++++++++---- src/main/resources/templates/MonitoredTripPush.ftl | 11 +++++++++++ 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/templates/MonitoredTripPush.ftl diff --git a/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java b/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java index e4e2b6466..778ea00c8 100644 --- a/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java +++ b/src/main/java/org/opentripplanner/middleware/models/MonitoredTrip.java @@ -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() { } diff --git a/src/main/java/org/opentripplanner/middleware/models/TripMonitorNotification.java b/src/main/java/org/opentripplanner/middleware/models/TripMonitorNotification.java index 94195bc82..5ea86804e 100644 --- a/src/main/java/org/opentripplanner/middleware/models/TripMonitorNotification.java +++ b/src/main/java/org/opentripplanner/middleware/models/TripMonitorNotification.java @@ -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; } diff --git a/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java b/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java index 9aa76531a..76e225f1d 100644 --- a/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java +++ b/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java @@ -141,7 +141,7 @@ private void addInitialReminderIfNeeded() { if (!trip.isInactive() && isFirstTimeCheckWithinLeadMonitoringTime && userWantsInitialReminder) { enqueueNotification( - TripMonitorNotification.createInitialReminderNotification() + TripMonitorNotification.createInitialReminderNotification(trip) ); } } @@ -428,6 +428,7 @@ private void sendNotifications() { } Map templateData = Map.of( "tripId", trip.id, + "tripName", trip.tripName, "notifications", notifications.stream() .map(notification -> notification.body) .collect(Collectors.toList()) @@ -465,7 +466,7 @@ private boolean sendSMS(OtpUser otpUser, Map data) { * Send push notification. */ private boolean sendPush(OtpUser otpUser, Map data) { - return NotificationUtils.sendPush(otpUser, "MonitoredTripText.ftl", data) != null; + return NotificationUtils.sendPush(otpUser, "MonitoredTripPush.ftl", data) != null; } /** diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index 4b42d9efd..2fe0f2681 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -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; @@ -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 @@ -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 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 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, diff --git a/src/main/resources/templates/MonitoredTripPush.ftl b/src/main/resources/templates/MonitoredTripPush.ftl new file mode 100644 index 000000000..070f3cf68 --- /dev/null +++ b/src/main/resources/templates/MonitoredTripPush.ftl @@ -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} + \ No newline at end of file