Skip to content

Commit

Permalink
Merge pull request #308 from 6QuizOnTheBlock/be/feat/#297-notice_fcm
Browse files Browse the repository at this point in the history
feat: μ•Œλ¦Όμž₯ μž‘μ„± μ‹œ FCM 전솑 κΈ°λŠ₯
  • Loading branch information
HABINOH authored May 13, 2024
2 parents 92fbba4 + bae69bb commit dfa05ee
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.quiz.ourclass.domain.notice.entity.Notice;
import com.quiz.ourclass.domain.notice.entity.NoticeType;
import com.quiz.ourclass.domain.notice.repository.NoticeRepository;
import com.quiz.ourclass.domain.organization.entity.MemberOrganization;
import com.quiz.ourclass.domain.organization.entity.Organization;
import com.quiz.ourclass.domain.organization.repository.OrganizationRepository;
import com.quiz.ourclass.global.dto.FcmDTO;
Expand Down Expand Up @@ -102,6 +103,33 @@ public Long write(Long organizationId, MultipartFile file, PostRequest request)
Post post = postMapper.postRequestToPost(
request, member, organization, now, image
);
//μ•Œλ¦Όμž₯ μž‘μ„± μ‹œ 단체 ν•™μƒλ“€μ—κ²Œ FCM λ°œμ†‘
if (post.getPostCategory() == PostCategory.NOTICE) {
String title = fcmUtil.makeNoticeTitle(
post.getOrganization().getName(), FcmType.NOTICE.getType()
);
String body = fcmUtil.makeNoticeBody(
post.getOrganization().getName(), FcmType.NOTICE.getType()
);
FcmDTO fcmDTO = fcmUtil.makeFcmDTO(title, body);

// μ•Œλ¦Ό μ €μž₯
Notice notice = Notice.builder()
.receiver(post.getOrganization().getManager())
.url(member.getName())
.content(body)
.type(NoticeType.NOTICE)
.createTime(LocalDateTime.now())
.build();
noticeRepository.save(notice);

fcmUtil.multiFcmSend(
organization.getMemberOrganizations().stream()
.map(MemberOrganization::getMember)
.toList(),
fcmDTO
);
}
return postRepository.save(post).getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.quiz.ourclass.domain.notice.entity;

public enum NoticeType {
TAG, RELAY, TALK, REPORT
TAG, RELAY, TALK, REPORT, NOTICE
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public abstract class ConstantUtil {
public static final int RELAY_DEMERIT = -100;
public static final int RELAY_REWARD = 50;
public static final Long RELAY_TIMEOUT_DAY = 1L;
public static final String FCM_KEY_PREFIX = "FCM_";
public static final int MAX_RETRIES = 5; //μ΅œλŒ€ μž¬μ‹œλ„ 횟수
public static final long INITIAL_BACKOFF = 1000L; //초기 λ°±μ˜€ν”„ μ‹œκ°„ (1초)

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum FcmType {
POST("κ²Œμ‹œκΈ€"),
COMMENT("λŒ“κΈ€"),
NOTICE("μ•Œλ¦Όμž₯"),
;
private final String Type;
}
19 changes: 12 additions & 7 deletions backEnd/src/main/java/com/quiz/ourclass/global/util/FcmUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
public class FcmUtil {

private final RedisUtil redisUtil;
private static final String FCM_KEY_PREFIX = "FCM_";
private static final int MAX_RETRIES = 5; //μ΅œλŒ€ μž¬μ‹œλ„ 횟수
private static final long INITIAL_BACKOFF = 1000L; //초기 λ°±μ˜€ν”„ μ‹œκ°„ (1초)

@Async("taskExecutor")
public void singleFcmSend(Member member, FcmDTO fcmDTO) {
Expand All @@ -38,7 +35,7 @@ public void multiFcmSend(List<Member> members, FcmDTO fcmDTO) {
}

private String getFcmRedisKey(Long memberId) {
return FCM_KEY_PREFIX + memberId;
return ConstantUtil.FCM_KEY_PREFIX + memberId;
}

public Message makeMessage(String title, String body, String token) {
Expand All @@ -55,17 +52,17 @@ public Message makeMessage(String title, String body, String token) {

private void sendMessage(Message message) {
int attempt = 0;
long backoff = INITIAL_BACKOFF;
long backoff = ConstantUtil.INITIAL_BACKOFF;

while (attempt < MAX_RETRIES) { //μ§€μˆ˜ λ°±μ˜€ν”„ μ „λž΅
while (attempt < ConstantUtil.MAX_RETRIES) { //μ§€μˆ˜ λ°±μ˜€ν”„ μ „λž΅
try {
FirebaseMessaging.getInstance().send(message);
log.info("FCM Send Success");
break; // 성곡 μ‹œ 루프 μ’…λ£Œ
} catch (FirebaseMessagingException e) {
log.error("FCM Send Error: {}", e.getMessage());
attempt++;
if (attempt >= MAX_RETRIES) {
if (attempt >= ConstantUtil.MAX_RETRIES) {
// μ΅œλŒ€ μž¬μ‹œλ„ 횟수 도달 μ‹œ 루프 μ’…λ£Œ
// λ‹€λ₯Έ λ©”μ‹œμ§€ μ‹œμŠ€ν…œμœΌλ‘œ μ•Œλ¦Όμ„ μ „μ†‘ν•˜λŠ” 방법을 κ³ λ €ν•΄λ³Ό 수 있음
log.error("Reached Maximum Retry Attempts");
Expand Down Expand Up @@ -97,4 +94,12 @@ public String makeReportTitle(String organizationName, String type) {
public String makeReportBody(String authorMember, String reportMember, String type) {
return authorMember + " 학생이 μž‘μ„±ν•œ " + type + "을" + reportMember + "학생이 μ‹ κ³ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.";
}

public String makeNoticeTitle(String organizationName, String type) {
return organizationName + " " + type;
}

public String makeNoticeBody(String organizationName, String type) {
return organizationName + " " + type + "이 λ“±λ‘λ˜μ—ˆμ–΄μš”!!";
}
}

0 comments on commit dfa05ee

Please sign in to comment.