Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom title and body for email notifications #476

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
343 changes: 258 additions & 85 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public Notification dtoToEntity(FcmNotificationDto notificationDto) {
.subtitle(notificationDto.getSubtitle())
.tag(notificationDto.getTag())
.emailEnabled(notificationDto.isEmailEnabled())
.emailTitle(notificationDto.getEmailTitle())
.emailBody(notificationDto.getEmailBody())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
package org.radarbase.appserver.converter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

import org.radarbase.appserver.dto.fcm.FcmUserDto;
import org.radarbase.appserver.entity.User;
import org.radarbase.appserver.entity.UserMetrics;
Expand Down Expand Up @@ -54,7 +53,7 @@ public User dtoToEntity(FcmUserDto fcmUserDto) {
return new User()
.setFcmToken(fcmUserDto.getFcmToken())
.setSubjectId(fcmUserDto.getSubjectId())
.setEmailAddress(fcmUserDto.getEmailAddress())
.setEmailAddress(fcmUserDto.getEmail())
.setUserMetrics(getValidUserMetrics(fcmUserDto))
.setEnrolmentDate(fcmUserDto.getEnrolmentDate())
.setTimezone(fcmUserDto.getTimezone())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public class FcmNotificationDto implements Serializable {

private boolean emailEnabled;

private String emailTitle;

private String emailBody;

private boolean mutableContent;

@DateTimeFormat(iso = ISO.DATE_TIME)
Expand Down Expand Up @@ -155,6 +159,8 @@ public FcmNotificationDto(Notification notificationEntity) {
this.tag = notificationEntity.getTag();
this.clickAction = notificationEntity.getClickAction();
this.emailEnabled = notificationEntity.isEmailEnabled();
this.emailTitle = notificationEntity.getEmailTitle();
this.emailBody = notificationEntity.getEmailBody();
this.mutableContent = notificationEntity.isMutableContent();
}

Expand Down Expand Up @@ -303,10 +309,20 @@ public FcmNotificationDto setClickAction(String clickAction) {
return this;
}

public FcmNotificationDto setEmailEnabled(boolean emailEnabled) {
this.emailEnabled = emailEnabled;
return this;
}
public FcmNotificationDto setEmailEnabled(boolean emailEnabled) {
this.emailEnabled = emailEnabled;
return this;
}

public FcmNotificationDto setEmailTitle(String emailTitle) {
this.emailTitle = emailTitle;
return this;
}

public FcmNotificationDto setEmailBody(String emailBody) {
this.emailBody = emailBody;
return this;
}

public FcmNotificationDto setMutableContent(boolean mutableContent) {
this.mutableContent = mutableContent;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/radarbase/appserver/dto/fcm/FcmUserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class FcmUserDto implements Serializable {

@Email
// Email address of the user (optional, needed when sending notifications via email)
private String emailAddress;
private String email;

// The most recent time when the app was opened
private Instant lastOpened;
Expand Down Expand Up @@ -93,6 +93,7 @@ public FcmUserDto(User user) {
this.id = user.getId();
this.projectId = user.getProject().getProjectId();
this.subjectId = user.getSubjectId();
this.email = user.getEmailAddress();
if (user.getUsermetrics() != null) {
this.lastOpened = user.getUsermetrics().getLastOpened();
this.lastDelivered = user.getUsermetrics().getLastDelivered();
Expand Down Expand Up @@ -135,8 +136,8 @@ public FcmUserDto setSubjectId(String subjectId) {
return this;
}

public FcmUserDto setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
public FcmUserDto setEmail(String email) {
this.email = email;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ public class EmailNotificationProtocol {

@JsonProperty("enabled")
private boolean enabled = false;

@JsonProperty("title")
private LanguageText title;

@JsonProperty("text")
private LanguageText body;

}
24 changes: 23 additions & 1 deletion src/main/java/org/radarbase/appserver/entity/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/**
* {@link Entity} for persisting notifications. The corresponding DTO is {@link FcmNotificationDto}.
* This also includes information for scheduling the notification through the Firebase Cloud
* Messaging(FCM) system.
* Messaging(FCM) system, and for sending email notifications.
*
* @author yatharthranjan
* @see Scheduled
Expand Down Expand Up @@ -123,6 +123,12 @@ public class Notification extends Message {
@Column(name = "email_enabled")
private boolean emailEnabled = false;

@Column(name = "email_title")
private String emailTitle;

@Column(name = "email_body")
private String emailBody;

@Nullable
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "additional_key", nullable = true)
Expand Down Expand Up @@ -164,6 +170,8 @@ public static class NotificationBuilder {
transient boolean emailEnabled;
transient Map<String, String> additionalData;
transient Task task;
transient String emailTitle;
transient String emailBody;


public NotificationBuilder(Notification notification) {
Expand Down Expand Up @@ -198,6 +206,8 @@ public NotificationBuilder(Notification notification) {
this.tag = notification.getTag();
this.clickAction = notification.getClickAction();
this.emailEnabled = notification.isEmailEnabled();
this.emailTitle = notification.getEmailTitle();
this.emailBody = notification.getEmailBody();
this.additionalData = notification.getAdditionalData();
this.task = notification.getTask();
}
Expand Down Expand Up @@ -353,6 +363,16 @@ public NotificationBuilder emailEnabled(boolean emailEnabled) {
return this;
}

public NotificationBuilder emailTitle(String title) {
this.emailTitle = title;
return this;
}

public NotificationBuilder emailBody(String body) {
this.emailBody = body;
return this;
}

public NotificationBuilder additionalData(Map<String, String> additionalData) {
this.additionalData = additionalData;
return this;
Expand Down Expand Up @@ -397,6 +417,8 @@ public Notification build() {
notification.setTag(this.tag);
notification.setClickAction(this.clickAction);
notification.setEmailEnabled(this.emailEnabled);
notification.setEmailTitle(this.emailTitle);
notification.setEmailBody(this.emailBody);
notification.setAdditionalData(this.additionalData);
notification.setTask(this.task);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.radarbase.appserver.converter.UserConverter;
import org.radarbase.appserver.dto.fcm.FcmUserDto;
import org.radarbase.appserver.dto.fcm.FcmUsers;
import org.radarbase.appserver.dto.questionnaire.Schedule;
import org.radarbase.appserver.entity.Project;
import org.radarbase.appserver.entity.User;
import org.radarbase.appserver.exception.InvalidUserDetailsException;
Expand Down Expand Up @@ -156,7 +155,7 @@ public FcmUserDto saveUserInProject(FcmUserDto userDto) {
+ ". Please use Update endpoint if need to update the user");
}

if (sendEmailNotifications && (userDto.getEmailAddress() == null || userDto.getEmailAddress().isEmpty())) {
if (sendEmailNotifications && (userDto.getEmail() == null || userDto.getEmail().isEmpty())) {
log.warn("No email address was provided for new subject '{}'. The option to send notifications via email " +
"('radar.notification.email.enabled') will not work for this subject. Consider to provide a valid email " +
"address for subject '{}'.", userDto.getSubjectId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.radarbase.appserver.service.transmitter;

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

private SimpleMailMessage createEmailFromNotification(Notification notification) {
String title = ObjectUtils.defaultIfNull(notification.getEmailTitle(), notification.getTitle());
String body = ObjectUtils.defaultIfNull(notification.getEmailBody(), notification.getBody());
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(notification.getUser().getEmailAddress());
message.setSubject(notification.getTitle());
message.setText(notification.getBody());
message.setSubject(title);
message.setText(body);
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ databaseChangeLog:
- column:
name: email
type: VARCHAR(255)
tableName: users
tableName: users
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ databaseChangeLog:
name: email_enabled
type: boolean
defaultValueBoolean: false
tableName: notifications
- column:
name: email_title
type: TEXT
- column:
name: email_body
type: TEXT
tableName: notifications
Loading