Skip to content

Commit 7f37c89

Browse files
authored
Merge pull request #476
Support custom title and body for email notifications
2 parents 9e45eb7 + 7550bd4 commit 7f37c89

File tree

11 files changed

+329
-102
lines changed

11 files changed

+329
-102
lines changed

README.md

Lines changed: 258 additions & 85 deletions
Large diffs are not rendered by default.

src/main/java/org/radarbase/appserver/converter/NotificationConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public Notification dtoToEntity(FcmNotificationDto notificationDto) {
6767
.subtitle(notificationDto.getSubtitle())
6868
.tag(notificationDto.getTag())
6969
.emailEnabled(notificationDto.isEmailEnabled())
70+
.emailTitle(notificationDto.getEmailTitle())
71+
.emailBody(notificationDto.getEmailBody())
7072
.build();
7173
}
7274

src/main/java/org/radarbase/appserver/converter/UserConverter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
package org.radarbase.appserver.converter;
2323

2424
import java.time.Instant;
25-
import java.time.LocalDateTime;
26-
import java.time.ZoneOffset;
25+
2726
import org.radarbase.appserver.dto.fcm.FcmUserDto;
2827
import org.radarbase.appserver.entity.User;
2928
import org.radarbase.appserver.entity.UserMetrics;
@@ -54,7 +53,7 @@ public User dtoToEntity(FcmUserDto fcmUserDto) {
5453
return new User()
5554
.setFcmToken(fcmUserDto.getFcmToken())
5655
.setSubjectId(fcmUserDto.getSubjectId())
57-
.setEmailAddress(fcmUserDto.getEmailAddress())
56+
.setEmailAddress(fcmUserDto.getEmail())
5857
.setUserMetrics(getValidUserMetrics(fcmUserDto))
5958
.setEnrolmentDate(fcmUserDto.getEnrolmentDate())
6059
.setTimezone(fcmUserDto.getTimezone())

src/main/java/org/radarbase/appserver/dto/fcm/FcmNotificationDto.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public class FcmNotificationDto implements Serializable {
111111

112112
private boolean emailEnabled;
113113

114+
private String emailTitle;
115+
116+
private String emailBody;
117+
114118
private boolean mutableContent;
115119

116120
@DateTimeFormat(iso = ISO.DATE_TIME)
@@ -155,6 +159,8 @@ public FcmNotificationDto(Notification notificationEntity) {
155159
this.tag = notificationEntity.getTag();
156160
this.clickAction = notificationEntity.getClickAction();
157161
this.emailEnabled = notificationEntity.isEmailEnabled();
162+
this.emailTitle = notificationEntity.getEmailTitle();
163+
this.emailBody = notificationEntity.getEmailBody();
158164
this.mutableContent = notificationEntity.isMutableContent();
159165
}
160166

@@ -303,10 +309,20 @@ public FcmNotificationDto setClickAction(String clickAction) {
303309
return this;
304310
}
305311

306-
public FcmNotificationDto setEmailEnabled(boolean emailEnabled) {
307-
this.emailEnabled = emailEnabled;
308-
return this;
309-
}
312+
public FcmNotificationDto setEmailEnabled(boolean emailEnabled) {
313+
this.emailEnabled = emailEnabled;
314+
return this;
315+
}
316+
317+
public FcmNotificationDto setEmailTitle(String emailTitle) {
318+
this.emailTitle = emailTitle;
319+
return this;
320+
}
321+
322+
public FcmNotificationDto setEmailBody(String emailBody) {
323+
this.emailBody = emailBody;
324+
return this;
325+
}
310326

311327
public FcmNotificationDto setMutableContent(boolean mutableContent) {
312328
this.mutableContent = mutableContent;

src/main/java/org/radarbase/appserver/dto/fcm/FcmUserDto.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class FcmUserDto implements Serializable {
6060

6161
@Email
6262
// Email address of the user (optional, needed when sending notifications via email)
63-
private String emailAddress;
63+
private String email;
6464

6565
// The most recent time when the app was opened
6666
private Instant lastOpened;
@@ -93,6 +93,7 @@ public FcmUserDto(User user) {
9393
this.id = user.getId();
9494
this.projectId = user.getProject().getProjectId();
9595
this.subjectId = user.getSubjectId();
96+
this.email = user.getEmailAddress();
9697
if (user.getUsermetrics() != null) {
9798
this.lastOpened = user.getUsermetrics().getLastOpened();
9899
this.lastDelivered = user.getUsermetrics().getLastDelivered();
@@ -135,8 +136,8 @@ public FcmUserDto setSubjectId(String subjectId) {
135136
return this;
136137
}
137138

138-
public FcmUserDto setEmailAddress(String emailAddress) {
139-
this.emailAddress = emailAddress;
139+
public FcmUserDto setEmail(String email) {
140+
this.email = email;
140141
return this;
141142
}
142143

src/main/java/org/radarbase/appserver/dto/protocol/EmailNotificationProtocol.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ public class EmailNotificationProtocol {
1717

1818
@JsonProperty("enabled")
1919
private boolean enabled = false;
20+
21+
@JsonProperty("title")
22+
private LanguageText title;
23+
24+
@JsonProperty("text")
25+
private LanguageText body;
2026

2127
}

src/main/java/org/radarbase/appserver/entity/Notification.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/**
4343
* {@link Entity} for persisting notifications. The corresponding DTO is {@link FcmNotificationDto}.
4444
* This also includes information for scheduling the notification through the Firebase Cloud
45-
* Messaging(FCM) system.
45+
* Messaging(FCM) system, and for sending email notifications.
4646
*
4747
* @author yatharthranjan
4848
* @see Scheduled
@@ -123,6 +123,12 @@ public class Notification extends Message {
123123
@Column(name = "email_enabled")
124124
private boolean emailEnabled = false;
125125

126+
@Column(name = "email_title")
127+
private String emailTitle;
128+
129+
@Column(name = "email_body")
130+
private String emailBody;
131+
126132
@Nullable
127133
@ElementCollection(fetch = FetchType.EAGER)
128134
@MapKeyColumn(name = "additional_key", nullable = true)
@@ -164,6 +170,8 @@ public static class NotificationBuilder {
164170
transient boolean emailEnabled;
165171
transient Map<String, String> additionalData;
166172
transient Task task;
173+
transient String emailTitle;
174+
transient String emailBody;
167175

168176

169177
public NotificationBuilder(Notification notification) {
@@ -198,6 +206,8 @@ public NotificationBuilder(Notification notification) {
198206
this.tag = notification.getTag();
199207
this.clickAction = notification.getClickAction();
200208
this.emailEnabled = notification.isEmailEnabled();
209+
this.emailTitle = notification.getEmailTitle();
210+
this.emailBody = notification.getEmailBody();
201211
this.additionalData = notification.getAdditionalData();
202212
this.task = notification.getTask();
203213
}
@@ -353,6 +363,16 @@ public NotificationBuilder emailEnabled(boolean emailEnabled) {
353363
return this;
354364
}
355365

366+
public NotificationBuilder emailTitle(String title) {
367+
this.emailTitle = title;
368+
return this;
369+
}
370+
371+
public NotificationBuilder emailBody(String body) {
372+
this.emailBody = body;
373+
return this;
374+
}
375+
356376
public NotificationBuilder additionalData(Map<String, String> additionalData) {
357377
this.additionalData = additionalData;
358378
return this;
@@ -397,6 +417,8 @@ public Notification build() {
397417
notification.setTag(this.tag);
398418
notification.setClickAction(this.clickAction);
399419
notification.setEmailEnabled(this.emailEnabled);
420+
notification.setEmailTitle(this.emailTitle);
421+
notification.setEmailBody(this.emailBody);
400422
notification.setAdditionalData(this.additionalData);
401423
notification.setTask(this.task);
402424

src/main/java/org/radarbase/appserver/service/UserService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.radarbase.appserver.converter.UserConverter;
2929
import org.radarbase.appserver.dto.fcm.FcmUserDto;
3030
import org.radarbase.appserver.dto.fcm.FcmUsers;
31-
import org.radarbase.appserver.dto.questionnaire.Schedule;
3231
import org.radarbase.appserver.entity.Project;
3332
import org.radarbase.appserver.entity.User;
3433
import org.radarbase.appserver.exception.InvalidUserDetailsException;
@@ -156,7 +155,7 @@ public FcmUserDto saveUserInProject(FcmUserDto userDto) {
156155
+ ". Please use Update endpoint if need to update the user");
157156
}
158157

159-
if (sendEmailNotifications && (userDto.getEmailAddress() == null || userDto.getEmailAddress().isEmpty())) {
158+
if (sendEmailNotifications && (userDto.getEmail() == null || userDto.getEmail().isEmpty())) {
160159
log.warn("No email address was provided for new subject '{}'. The option to send notifications via email " +
161160
"('radar.notification.email.enabled') will not work for this subject. Consider to provide a valid email " +
162161
"address for subject '{}'.", userDto.getSubjectId());

src/main/java/org/radarbase/appserver/service/transmitter/EmailNotificationTransmitter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.radarbase.appserver.service.transmitter;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.commons.lang3.ObjectUtils;
45
import org.radarbase.appserver.entity.Notification;
56
import org.radarbase.appserver.exception.EmailMessageTransmitException;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,11 +45,13 @@ public void send(Notification notification) throws EmailMessageTransmitException
4445
}
4546

4647
private SimpleMailMessage createEmailFromNotification(Notification notification) {
48+
String title = ObjectUtils.defaultIfNull(notification.getEmailTitle(), notification.getTitle());
49+
String body = ObjectUtils.defaultIfNull(notification.getEmailBody(), notification.getBody());
4750
SimpleMailMessage message = new SimpleMailMessage();
4851
message.setFrom(from);
4952
message.setTo(notification.getUser().getEmailAddress());
50-
message.setSubject(notification.getTitle());
51-
message.setText(notification.getBody());
53+
message.setSubject(title);
54+
message.setText(body);
5255
return message;
5356
}
5457
}

src/main/resources/db/changelog/changes/00000000000002_update_schema-20240704090100_changelog.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ databaseChangeLog:
88
- column:
99
name: email
1010
type: VARCHAR(255)
11-
tableName: users
11+
tableName: users

0 commit comments

Comments
 (0)