feat: improve push notifications

This commit is contained in:
Tim
2025-07-30 11:53:43 +08:00
parent fe79a5481a
commit 40c919348f
5 changed files with 69 additions and 17 deletions

View File

@@ -7,6 +7,8 @@ import lombok.RequiredArgsConstructor;
import com.openisle.service.EmailSender;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
@@ -22,6 +24,21 @@ public class NotificationService {
@Value("${app.website-url}")
private String websiteUrl;
private String buildPayload(String body, String url) {
try {
return new ObjectMapper().writeValueAsString(Map.of(
"body", body,
"url", url
));
} catch (Exception e) {
return body;
}
}
public void sendCustomPush(User user, String body, String url) {
pushNotificationService.sendNotification(user, buildPayload(body, url));
}
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved) {
return createNotification(user, type, post, comment, approved, null, null, null);
}
@@ -38,10 +55,16 @@ public class NotificationService {
n.setReactionType(reactionType);
n.setContent(content);
n = notificationRepository.save(n);
pushNotificationService.sendNotification(user, "You have a new notification");
String body = "You have a new notification";
String url = websiteUrl + "/messages";
if (type == NotificationType.COMMENT_REPLY && post != null && comment != null) {
body = "有人回复了你";
url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId());
}
sendCustomPush(user, body, url);
if (type == NotificationType.COMMENT_REPLY && user.getEmail() != null && post != null && comment != null) {
String url = String.format("%s/posts/%d#comment-%d", websiteUrl, post.getId(), comment.getId());
emailSender.sendEmail(user.getEmail(), "【OpenIsle】有人回复了你", url);
}

View File

@@ -48,9 +48,12 @@ public class ReactionService {
if (!user.getId().equals(post.getAuthor().getId())) {
notificationService.createNotification(post.getAuthor(), NotificationType.REACTION, post, null, null, user, type, null);
long count = reactionRepository.countReceived(post.getAuthor().getUsername());
if (count % 5 == 0 && post.getAuthor().getEmail() != null) {
if (count % 5 == 0) {
String url = websiteUrl + "/messages";
emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
notificationService.sendCustomPush(post.getAuthor(), "你有新的互动", url);
if (post.getAuthor().getEmail() != null) {
emailSender.sendEmail(post.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
}
}
}
return reaction;
@@ -76,9 +79,12 @@ public class ReactionService {
if (!user.getId().equals(comment.getAuthor().getId())) {
notificationService.createNotification(comment.getAuthor(), NotificationType.REACTION, comment.getPost(), comment, null, user, type, null);
long count = reactionRepository.countReceived(comment.getAuthor().getUsername());
if (count % 5 == 0 && comment.getAuthor().getEmail() != null) {
if (count % 5 == 0) {
String url = websiteUrl + "/messages";
emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
notificationService.sendCustomPush(comment.getAuthor(), "你有新的互动", url);
if (comment.getAuthor().getEmail() != null) {
emailSender.sendEmail(comment.getAuthor().getEmail(), "【OpenIsle】你有新的互动", url);
}
}
}
return reaction;