This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ user 엔터티 fcmToken 컬럼 추가, 로그인 시 토큰 최신화 구현 (#64)
* Initial commit * Feat/#1 oauth2login (#3) * feat: User 엔터티 생성 * feat: jwt 버전 11->12, JWTUtil 생성 * feat: JWTFilter(JwtAuthenticationFilter) 등록 * feat: kakao 로그인 구현 * docs: swagger 태그(Authorization) 추가 (#5) * feat: User 엔터티 생성 * feat: jwt 버전 11->12, JWTUtil 생성 * feat: JWTFilter(JwtAuthenticationFilter) 등록 * feat: kakao 로그인 구현 * docs: swagger 태그(Authorization) 추가 * feat: accesstoken 테스트를 위한 test login 생성 (#9) * feat: User 엔티티에 상속 (#12) * feat: BaseEntity 생성 * feat: User 엔티티에 상속 * feat: 일기 생성 기능 구현 (#14) * feat: accesstoken 테스트를 위한 test login 생성 * feat: 일기 생성 기능 구현 * hotfix: ci 에러 수정 (#16) * feat: accesstoken 테스트를 위한 test login 생성 * feat: 일기 생성 기능 구현 * hotfix: ci 에러 수정 * fix: OIDC 카카오 로그인 nullPointerException 해결 * feat: 닉네임 설정 기능 구현 (#21) * feat: 일기 수정 기능 구현 (#25) * feat: 일기에 감정 컬럼 추가 * feat: 일기 수정 기능 구현 * feat: 일기 삭제 기능 구현 (#27) * feat: 일기에 감정 컬럼 추가 * feat: 일기 수정 기능 구현 * feat: 일기 삭제 기능 구현 * feat: 일기 감정 분석 기능 구현 (#31) * feat: 감정 저장 기능 구현 (#33) * feat: 일기 감정 분석 기능 구현 * feat: 감정 저장 기능 구현 * fix: 🐛 감정 저장 안되던 오류 수정 (#35) * feat: 일기 감정 분석 기능 구현 * feat: 감정 저장 기능 구현 * fix: 🐛 감정 저장 안되던 오류 수정 * hotfix: 🚑 서버 꺼짐 현상 해결 (#37) * feat: 일기 감정 분석 기능 구현 * feat: 감정 저장 기능 구현 * fix: 🐛 감정 저장 안되던 오류 수정 * hotfix: 🚑 서버 꺼짐 현상 해결 * feat: ✨ 홈 화면 조회 기능 구현 (#41) * feat: ✨ 회원가입 완료 여부 필드 추가 (#44) * feat: ✨ 일기 상세 조회 구현 (#47) * feat: ✨ 기간 별 감정 통계 조회 기능 구현 (#50) * feat: ✨ 일기 내용 검색 기능 구현 (#52) * feat: ✨ 감정 별 일기 조회 (#54) * feat: ✨ 월 별 일기 조회 기능 구현 (#59) * ci: ⚡ workflow 수정 (#61) * ci: ⚡ workflow 수정 * ci: ⚡ workflow 수정 * feat: ✨ user 엔터티 fcmToken 컬럼 추가, 로그인 시 토큰 최신화 구현 (#63) * ci: ⚡ workflow 수정 * ci: ⚡ workflow 수정 * feat: ✨ fcm 토큰 알림 기능 구현 * feat: ✨ user 엔터티 fcmToken 컬럼 추가, 로그인 시 토큰 최신화 구현
- Loading branch information
1 parent
8e0294b
commit 56b016e
Showing
9 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
@Builder | ||
public record IdTokenReq( | ||
String idToken, | ||
String provider | ||
String provider, | ||
String fcmToken | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/com/aidiary/domain/notification/application/FcmService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.aidiary.domain.notification.application; | ||
|
||
import com.aidiary.domain.notification.dto.FcmMessage; | ||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import lombok.RequiredArgsConstructor; | ||
import okhttp3.*; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class FcmService { | ||
|
||
private final String API_URL = "https://fcm.googleapis.com/v1/projects/" + | ||
"nabi-ffce7/messages:send"; | ||
private final ObjectMapper objectMapper; | ||
|
||
@Transactional | ||
public void sendMessageTo(String targetToken, String title, String body) throws IOException { | ||
String message = makeMessage(targetToken, title, body); | ||
|
||
OkHttpClient client = new OkHttpClient(); | ||
RequestBody requestBody = RequestBody.create(message, | ||
MediaType.get("application/json; charset=utf-8")); | ||
Request request = new Request.Builder() | ||
.url(API_URL) | ||
.post(requestBody) | ||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()) | ||
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8") | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
|
||
System.out.println(response.body().string()); | ||
} | ||
|
||
private String makeMessage(String targetToken, String title, String body) throws JsonParseException, JsonProcessingException { | ||
FcmMessage fcmMessage = FcmMessage.builder() | ||
.message(FcmMessage.Msg.builder() | ||
.token(targetToken) | ||
.notification(FcmMessage.Notification.builder() | ||
.title(title) | ||
.body(body) | ||
.image(null) | ||
.build() | ||
).build()).validateOnly(false).build(); | ||
|
||
return objectMapper.writeValueAsString(fcmMessage); | ||
} | ||
|
||
private String getAccessToken() throws IOException { | ||
String firebaseConfigPath = "firebase/firebase_service_key.json"; | ||
|
||
GoogleCredentials googleCredentials = GoogleCredentials | ||
.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream()) | ||
.createScoped(List.of("https://www.googleapis.com/auth/cloud-platform")); | ||
|
||
googleCredentials.refreshIfExpired(); | ||
return googleCredentials.getAccessToken().getTokenValue(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/aidiary/domain/notification/dto/FcmMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.aidiary.domain.notification.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
public record FcmMessage( | ||
boolean validateOnly, | ||
Msg message | ||
) { | ||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Msg { | ||
private Notification notification; | ||
private String token; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Notification { | ||
private String title; | ||
private String body; | ||
private String image; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/aidiary/domain/notification/dto/FcmReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.aidiary.domain.notification.dto; | ||
|
||
public record FcmReq( | ||
String targetToken, | ||
String title, | ||
String body | ||
) { | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/aidiary/domain/notification/presentation/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.aidiary.domain.notification.presentation; | ||
|
||
import com.aidiary.domain.notification.application.FcmService; | ||
import com.aidiary.domain.notification.dto.FcmReq; | ||
import com.aidiary.global.payload.ResponseCustom; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/fcm") | ||
public class NotificationController { | ||
|
||
private final FcmService fcmService; | ||
|
||
|
||
@PostMapping | ||
public ResponseCustom<?> pushMessage(@RequestBody FcmReq fcmReq) throws IOException { | ||
System.out.println(fcmReq.targetToken() + " " | ||
+ fcmReq.title() + " " + fcmReq.body()); | ||
|
||
fcmService.sendMessageTo( | ||
fcmReq.targetToken(), | ||
fcmReq.title(), | ||
fcmReq.body() | ||
); | ||
|
||
return ResponseCustom.OK("Message is pushed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters