diff options
28 files changed, 55 insertions, 33 deletions
diff --git a/src/main/java/org/traccar/api/resource/PasswordResource.java b/src/main/java/org/traccar/api/resource/PasswordResource.java index 20e8d768d..1868a6191 100644 --- a/src/main/java/org/traccar/api/resource/PasswordResource.java +++ b/src/main/java/org/traccar/api/resource/PasswordResource.java @@ -19,7 +19,7 @@ import org.apache.velocity.VelocityContext; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.User; -import org.traccar.notification.FullMessage; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.TextTemplateFormatter; import javax.annotation.security.PermitAll; @@ -53,8 +53,9 @@ public class PasswordResource extends BaseResource { Context.getUsersManager().updateItem(user); VelocityContext velocityContext = TextTemplateFormatter.prepareContext(null); velocityContext.put("token", token); - FullMessage message = TextTemplateFormatter.formatFullMessage(velocityContext, "passwordReset"); - Context.getMailManager().sendMessage(userId, message.getSubject(), message.getBody()); + NotificationMessage fullMessage = + TextTemplateFormatter.formatMessage(velocityContext, "passwordReset", "full"); + Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody()); break; } } diff --git a/src/main/java/org/traccar/notification/NotificationFormatter.java b/src/main/java/org/traccar/notification/NotificationFormatter.java index dabc75b8b..9a6723a71 100644 --- a/src/main/java/org/traccar/notification/NotificationFormatter.java +++ b/src/main/java/org/traccar/notification/NotificationFormatter.java @@ -58,14 +58,9 @@ public final class NotificationFormatter { return velocityContext; } - public static FullMessage formatFullMessage(long userId, Event event, Position position) { + public static NotificationMessage formatMessage(long userId, Event event, Position position, String templatePath) { VelocityContext velocityContext = prepareContext(userId, event, position); - return TextTemplateFormatter.formatFullMessage(velocityContext, event.getType()); - } - - public static String formatShortMessage(long userId, Event event, Position position) { - VelocityContext velocityContext = prepareContext(userId, event, position); - return TextTemplateFormatter.formatShortMessage(velocityContext, event.getType()); + return TextTemplateFormatter.formatMessage(velocityContext, event.getType(), templatePath); } } diff --git a/src/main/java/org/traccar/notification/FullMessage.java b/src/main/java/org/traccar/notification/NotificationMessage.java index f66537c6e..0fb8d7654 100644 --- a/src/main/java/org/traccar/notification/FullMessage.java +++ b/src/main/java/org/traccar/notification/NotificationMessage.java @@ -16,12 +16,12 @@ */ package org.traccar.notification; -public class FullMessage { +public class NotificationMessage { private String subject; private String body; - public FullMessage(String subject, String body) { + public NotificationMessage(String subject, String body) { this.subject = subject; this.body = body; } diff --git a/src/main/java/org/traccar/notification/TextTemplateFormatter.java b/src/main/java/org/traccar/notification/TextTemplateFormatter.java index c7cac2d4d..b7058c824 100644 --- a/src/main/java/org/traccar/notification/TextTemplateFormatter.java +++ b/src/main/java/org/traccar/notification/TextTemplateFormatter.java @@ -71,21 +71,10 @@ public final class TextTemplateFormatter { return template; } - public static FullMessage formatFullMessage(VelocityContext velocityContext, String name) { - String formattedMessage = formatMessage(velocityContext, name, "full"); - return new FullMessage((String) velocityContext.get("subject"), formattedMessage); - } - - public static String formatShortMessage(VelocityContext velocityContext, String name) { - return formatMessage(velocityContext, name, "short"); - } - - private static String formatMessage( - VelocityContext velocityContext, String name, String templatePath) { - + public static NotificationMessage formatMessage(VelocityContext velocityContext, String name, String templatePath) { StringWriter writer = new StringWriter(); getTemplate(name, templatePath).merge(velocityContext, writer); - return writer.toString(); + return new NotificationMessage((String) velocityContext.get("subject"), writer.toString()); } } diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java index 78d5da1e2..f91ec25a0 100644 --- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java +++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java @@ -24,6 +24,7 @@ import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.NotificationFormatter; import javax.ws.rs.client.Entity; @@ -37,6 +38,8 @@ public class NotificatorFirebase extends Notificator { private final String key; public static class Notification { + @JsonProperty("title") + private String title; @JsonProperty("body") private String body; @JsonProperty("sound") @@ -66,8 +69,11 @@ public class NotificatorFirebase extends Notificator { final User user = Context.getPermissionsManager().getUser(userId); if (user.getAttributes().containsKey("notificationTokens")) { + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + Notification notification = new Notification(); - notification.body = NotificationFormatter.formatShortMessage(userId, event, position).trim(); + notification.title = shortMessage.getSubject(); + notification.body = shortMessage.getBody(); notification.sound = "default"; Message message = new Message(); diff --git a/src/main/java/org/traccar/notificators/NotificatorMail.java b/src/main/java/org/traccar/notificators/NotificatorMail.java index 6b9774c58..9b5637ed8 100644 --- a/src/main/java/org/traccar/notificators/NotificatorMail.java +++ b/src/main/java/org/traccar/notificators/NotificatorMail.java @@ -19,7 +19,7 @@ package org.traccar.notificators; import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.notification.FullMessage; +import org.traccar.notification.NotificationMessage; import org.traccar.notification.MessageException; import org.traccar.notification.NotificationFormatter; @@ -30,8 +30,8 @@ public final class NotificatorMail extends Notificator { @Override public void sendSync(long userId, Event event, Position position) throws MessageException { try { - FullMessage message = NotificationFormatter.formatFullMessage(userId, event, position); - Context.getMailManager().sendMessage(userId, message.getSubject(), message.getBody()); + NotificationMessage fullMessage = NotificationFormatter.formatMessage(userId, event, position, "full"); + Context.getMailManager().sendMessage(userId, fullMessage.getSubject(), fullMessage.getBody()); } catch (MessagingException e) { throw new MessageException(e); } diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 189af7834..456c2fe4f 100644 --- a/src/main/java/org/traccar/notificators/NotificatorPushover.java +++ b/src/main/java/org/traccar/notificators/NotificatorPushover.java @@ -24,6 +24,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -43,6 +44,8 @@ public class NotificatorPushover extends Notificator { private String user; @JsonProperty("device") private String device; + @JsonProperty("title") + private String title; @JsonProperty("message") private String message; } @@ -74,11 +77,14 @@ public class NotificatorPushover extends Notificator { return; } + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + Message message = new Message(); message.token = token; message.user = this.user; message.device = device; - message.message = NotificationFormatter.formatShortMessage(userId, event, position); + message.title = shortMessage.getSubject(); + message.message = shortMessage.getBody(); Context.getClient().target(url).request() .async().post(Entity.json(message), new InvocationCallback<Object>() { diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java index 8124e40b1..fb817b112 100644 --- a/src/main/java/org/traccar/notificators/NotificatorSms.java +++ b/src/main/java/org/traccar/notificators/NotificatorSms.java @@ -24,6 +24,7 @@ import org.traccar.model.Position; import org.traccar.model.User; import org.traccar.notification.MessageException; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; public final class NotificatorSms extends Notificator { @@ -31,9 +32,10 @@ public final class NotificatorSms extends Notificator { public void sendAsync(long userId, Event event, Position position) { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); Main.getInjector().getInstance(StatisticsManager.class).registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), - NotificationFormatter.formatShortMessage(userId, event, position), false); + shortMessage.getBody(), false); } } @@ -41,9 +43,10 @@ public final class NotificatorSms extends Notificator { public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); Main.getInjector().getInstance(StatisticsManager.class).registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), - NotificationFormatter.formatShortMessage(userId, event, position), false); + shortMessage.getBody(), false); } } diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index dbba0d31d..70148110c 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -25,6 +25,7 @@ import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.NotificationMessage; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -98,12 +99,14 @@ public class NotificatorTelegram extends Notificator { @Override public void sendSync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); + NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short"); + TextMessage message = new TextMessage(); message.chatId = user.getString("telegramChatId"); if (message.chatId == null) { message.chatId = chatId; } - message.text = NotificationFormatter.formatShortMessage(userId, event, position); + message.text = shortMessage.getBody(); executeRequest(urlSendText, message); if (sendLocation && position != null) { executeRequest(urlSendLocation, createLocationMessage(message.chatId, position)); diff --git a/templates/short/alarm.vm b/templates/short/alarm.vm index ce641781b..15970dab8 100644 --- a/templates/short/alarm.vm +++ b/templates/short/alarm.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: alarm!") $device.name alarm: $position.getString("alarm") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/commandResult.vm b/templates/short/commandResult.vm index 27fd668be..6dc7a5dec 100644 --- a/templates/short/commandResult.vm +++ b/templates/short/commandResult.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: command result received") $device.name command result received: $position.getString("result") at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceFuelDrop.vm b/templates/short/deviceFuelDrop.vm index 5eabe9cc0..babe14351 100644 --- a/templates/short/deviceFuelDrop.vm +++ b/templates/short/deviceFuelDrop.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: fuel drop") $device.name fuel drop at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceInactive.vm b/templates/short/deviceInactive.vm index d7431124c..c293bf1b2 100644 --- a/templates/short/deviceInactive.vm +++ b/templates/short/deviceInactive.vm @@ -1,3 +1,4 @@ +#set($subject = "$device.name: inactive") #set($lastUpdate = $dateTool.getDate()) #set($ignore = $lastUpdate.setTime($event.getLong("lastUpdate"))) $device.name inactive from $dateTool.format("YYYY-MM-dd HH:mm:ss", $lastUpdate, $locale, $timezone) diff --git a/templates/short/deviceMoving.vm b/templates/short/deviceMoving.vm index a08becd2b..bf6aec340 100644 --- a/templates/short/deviceMoving.vm +++ b/templates/short/deviceMoving.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: moving") $device.name moving at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceOffline.vm b/templates/short/deviceOffline.vm index 1dac43ac6..e663812ab 100644 --- a/templates/short/deviceOffline.vm +++ b/templates/short/deviceOffline.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: offline") $device.name offline at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceOnline.vm b/templates/short/deviceOnline.vm index 3c18097c1..bf3b40096 100644 --- a/templates/short/deviceOnline.vm +++ b/templates/short/deviceOnline.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: online") $device.name online at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceOverspeed.vm b/templates/short/deviceOverspeed.vm index fe318fe08..849c6ddb7 100644 --- a/templates/short/deviceOverspeed.vm +++ b/templates/short/deviceOverspeed.vm @@ -1,3 +1,4 @@ +#set($subject = "$device.name: exceeds the speed") #if($speedUnit == 'kmh') #set($speedValue = $position.speed * 1.852) #set($speedString = $numberTool.format("0.0 km/h", $speedValue)) diff --git a/templates/short/deviceStopped.vm b/templates/short/deviceStopped.vm index 1a63630ba..8fabf89f1 100644 --- a/templates/short/deviceStopped.vm +++ b/templates/short/deviceStopped.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: stopped") $device.name stopped at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/deviceUnknown.vm b/templates/short/deviceUnknown.vm index 37baa30a7..b6a6e9c9f 100644 --- a/templates/short/deviceUnknown.vm +++ b/templates/short/deviceUnknown.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: status is unknown") $device.name status is unknown at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/driverChanged.vm b/templates/short/driverChanged.vm index 36ed8955e..df96b00a1 100644 --- a/templates/short/driverChanged.vm +++ b/templates/short/driverChanged.vm @@ -1,3 +1,4 @@ +#set($subject = "$device.name: driver has changed") #if($driver) #set($driverName = $driver.name) #else diff --git a/templates/short/geofenceEnter.vm b/templates/short/geofenceEnter.vm index 962eecc53..8c250665e 100644 --- a/templates/short/geofenceEnter.vm +++ b/templates/short/geofenceEnter.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: has entered geofence") $device.name has entered geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/geofenceExit.vm b/templates/short/geofenceExit.vm index 47999fa94..7d3ae6f6e 100644 --- a/templates/short/geofenceExit.vm +++ b/templates/short/geofenceExit.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: has exited geofence") $device.name has exited geofence $geofence.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/ignitionOff.vm b/templates/short/ignitionOff.vm index 4d4b45ccc..db5a3f3e1 100644 --- a/templates/short/ignitionOff.vm +++ b/templates/short/ignitionOff.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: ignition OFF") $device.name ignition OFF at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/ignitionOn.vm b/templates/short/ignitionOn.vm index d8dd07966..412ad4d84 100644 --- a/templates/short/ignitionOn.vm +++ b/templates/short/ignitionOn.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: ignition ON") $device.name ignition ON at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/maintenance.vm b/templates/short/maintenance.vm index a931e5c20..a58e274b8 100644 --- a/templates/short/maintenance.vm +++ b/templates/short/maintenance.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: maintenance is required") $device.name maintenance $maintenance.name is required at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/test.vm b/templates/short/test.vm index d25f218ce..c9d93ce67 100644 --- a/templates/short/test.vm +++ b/templates/short/test.vm @@ -1 +1,2 @@ +#set($subject = "Test message") Test message diff --git a/templates/short/textMessage.vm b/templates/short/textMessage.vm index 59fa7cbc6..54c134df4 100644 --- a/templates/short/textMessage.vm +++ b/templates/short/textMessage.vm @@ -1 +1,2 @@ +#set($subject = "$device.name: text message received") Text message received from $device.name at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) diff --git a/templates/short/unknown.vm b/templates/short/unknown.vm index fd20b50cc..2f9d5e3af 100644 --- a/templates/short/unknown.vm +++ b/templates/short/unknown.vm @@ -1 +1,2 @@ +#set($subject = "Unknown type") Unknown type |