From 12b26830b3eae6fac9e87c462fc09cfe6c962964 Mon Sep 17 00:00:00 2001 From: Khaksar Weqar Date: Tue, 2 Nov 2021 16:22:03 +0100 Subject: short-message-title-from-templates --- src/main/java/org/traccar/config/Keys.java | 7 ----- .../notification/NotificationFormatter.java | 2 +- .../org/traccar/notification/ShortMessage.java | 36 ++++++++++++++++++++++ .../notification/TextTemplateFormatter.java | 5 +-- .../traccar/notificators/NotificatorFirebase.java | 8 +++-- .../traccar/notificators/NotificatorPushover.java | 8 ++++- .../org/traccar/notificators/NotificatorSms.java | 7 +++-- .../traccar/notificators/NotificatorTelegram.java | 5 ++- 8 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/traccar/notification/ShortMessage.java (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index f037c632f..1411e8a13 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -744,13 +744,6 @@ public final class Keys { "notificator.firebase.key", Collections.singletonList(KeyType.GLOBAL)); - /** - * Firebase push notifications title. - */ - public static final ConfigKey NOTIFICATOR_FIREBASE_TITLE = new ConfigKey<>( - "notificator.firebase.title", - Collections.singletonList(KeyType.GLOBAL)); - /** * Pushover notification user name. */ diff --git a/src/main/java/org/traccar/notification/NotificationFormatter.java b/src/main/java/org/traccar/notification/NotificationFormatter.java index dabc75b8b..f33961c8b 100644 --- a/src/main/java/org/traccar/notification/NotificationFormatter.java +++ b/src/main/java/org/traccar/notification/NotificationFormatter.java @@ -63,7 +63,7 @@ public final class NotificationFormatter { return TextTemplateFormatter.formatFullMessage(velocityContext, event.getType()); } - public static String formatShortMessage(long userId, Event event, Position position) { + public static ShortMessage formatShortMessage(long userId, Event event, Position position) { VelocityContext velocityContext = prepareContext(userId, event, position); return TextTemplateFormatter.formatShortMessage(velocityContext, event.getType()); } diff --git a/src/main/java/org/traccar/notification/ShortMessage.java b/src/main/java/org/traccar/notification/ShortMessage.java new file mode 100644 index 000000000..a0ee61def --- /dev/null +++ b/src/main/java/org/traccar/notification/ShortMessage.java @@ -0,0 +1,36 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.notification; + +public class ShortMessage { + + private String title; + private String body; + + public ShortMessage(String subject, String body) { + this.title = subject; + this.body = body; + } + + public String getTitle() { + return title; + } + + public String getBody() { + return body; + } +} diff --git a/src/main/java/org/traccar/notification/TextTemplateFormatter.java b/src/main/java/org/traccar/notification/TextTemplateFormatter.java index c7cac2d4d..8a4d35677 100644 --- a/src/main/java/org/traccar/notification/TextTemplateFormatter.java +++ b/src/main/java/org/traccar/notification/TextTemplateFormatter.java @@ -76,8 +76,9 @@ public final class TextTemplateFormatter { return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } - public static String formatShortMessage(VelocityContext velocityContext, String name) { - return formatMessage(velocityContext, name, "short"); + public static ShortMessage formatShortMessage(VelocityContext velocityContext, String name) { + String formattedMessage = formatMessage(velocityContext, name, "short"); + return new ShortMessage((String) velocityContext.get("title"), formattedMessage); } private static String formatMessage( diff --git a/src/main/java/org/traccar/notificators/NotificatorFirebase.java b/src/main/java/org/traccar/notificators/NotificatorFirebase.java index cd084080e..9d1982f9c 100644 --- a/src/main/java/org/traccar/notificators/NotificatorFirebase.java +++ b/src/main/java/org/traccar/notificators/NotificatorFirebase.java @@ -24,7 +24,9 @@ import org.traccar.config.Keys; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; +import org.traccar.notification.FullMessage; import org.traccar.notification.NotificationFormatter; +import org.traccar.notification.ShortMessage; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -68,9 +70,11 @@ public class NotificatorFirebase extends Notificator { final User user = Context.getPermissionsManager().getUser(userId); if (user.getAttributes().containsKey("notificationTokens")) { + ShortMessage shortMessage = NotificationFormatter.formatShortMessage(userId, event, position); + Notification notification = new Notification(); - notification.title=Context.getConfig().getString(Keys.NOTIFICATOR_FIREBASE_TITLE); - notification.body = NotificationFormatter.formatShortMessage(userId, event, position).trim(); + notification.title= shortMessage.getTitle(); + notification.body = shortMessage.getBody(); notification.sound = "default"; Message message = new Message(); diff --git a/src/main/java/org/traccar/notificators/NotificatorPushover.java b/src/main/java/org/traccar/notificators/NotificatorPushover.java index 189af7834..e541ea525 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.ShortMessage; 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; } + ShortMessage shortMessage = NotificationFormatter.formatShortMessage(userId, event, position); + Message message = new Message(); message.token = token; message.user = this.user; message.device = device; - message.message = NotificationFormatter.formatShortMessage(userId, event, position); + message.title= shortMessage.getTitle(); + message.message = shortMessage.getBody(); Context.getClient().target(url).request() .async().post(Entity.json(message), new InvocationCallback() { diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java index 8124e40b1..360eb44ff 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.ShortMessage; 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) { + ShortMessage shortMessage = NotificationFormatter.formatShortMessage(userId, event, position); 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) { + ShortMessage shortMessage = NotificationFormatter.formatShortMessage(userId, event, position); 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..a8cdacfbf 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.ShortMessage; 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); + ShortMessage shortMessage = NotificationFormatter.formatShortMessage(userId, event, position); + 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)); -- cgit v1.2.3