-
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.
Merge pull request #34 from Central-MakeUs/feature/33
Feature/33: FCM 푸시알림 기능 구현-1
- Loading branch information
Showing
15 changed files
with
278 additions
and
29 deletions.
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
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
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/cmc/suppin/fcm/config/FirebaseConfig.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,34 @@ | ||
package com.cmc.suppin.fcm.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
|
||
@Value("${FIREBASE_CONFIG_PATH}") | ||
private String firebaseConfigPath; | ||
|
||
@PostConstruct | ||
public void init() { | ||
try { | ||
InputStream serviceAccount = new FileInputStream(firebaseConfigPath); | ||
FirebaseOptions options = new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.build(); | ||
|
||
if (FirebaseApp.getApps().isEmpty()) { // FirebaseApp이 이미 초기화되어 있지 않은 경우에만 초기화 실행 | ||
FirebaseApp.initializeApp(options); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/cmc/suppin/fcm/controller/FcmController.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,37 @@ | ||
package com.cmc.suppin.fcm.controller; | ||
|
||
import com.cmc.suppin.fcm.controller.dto.FcmSendDTO; | ||
import com.cmc.suppin.fcm.service.FcmService; | ||
import com.cmc.suppin.global.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
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; | ||
|
||
@Slf4j | ||
@RestController | ||
@Tag(name = "FCM", description = "FCM 푸시알림 관련 API") | ||
@RequestMapping("/api/v1/fcm") | ||
public class FcmController { | ||
|
||
private final FcmService fcmService; | ||
|
||
public FcmController(FcmService fcmService) { | ||
this.fcmService = fcmService; | ||
} | ||
|
||
// 모바일로부터 사용자 FCM 토큰, 메시지 제목, 내용을 받아서 서비스를 처리 | ||
@PostMapping("/send") | ||
public ResponseEntity<ApiResponse<Object>> pushMessage(@RequestBody @Validated FcmSendDTO fcmSendDto) throws IOException { | ||
log.debug("[+] 푸시 메시지를 전송합니다. "); | ||
int result = fcmService.sendMessageTo(fcmSendDto); | ||
|
||
return ResponseEntity.ok(ApiResponse.of(result)); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/cmc/suppin/fcm/controller/dto/FcmMessageDTO.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,32 @@ | ||
package com.cmc.suppin.fcm.controller.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
/** | ||
* FCM에 실제 전송될 데이터의 DTO | ||
*/ | ||
@Getter | ||
@Builder | ||
public class FcmMessageDTO { | ||
private boolean validateOnly; | ||
private FcmMessageDTO.Message message; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Message { | ||
private FcmMessageDTO.Notification notification; | ||
private String token; | ||
} | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
public static class Notification { | ||
private String title; | ||
private String body; | ||
private String image; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/cmc/suppin/fcm/controller/dto/FcmSendDTO.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,24 @@ | ||
package com.cmc.suppin.fcm.controller.dto; | ||
|
||
import lombok.*; | ||
|
||
/** | ||
* 모바일에서 전달받은 객체를 FCM으로 전송하기 위한 DTO | ||
*/ | ||
@Getter | ||
@ToString | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class FcmSendDTO { | ||
private String token; | ||
|
||
private String title; | ||
|
||
private String body; | ||
|
||
@Builder(toBuilder = true) | ||
public FcmSendDTO(String token, String title, String body) { | ||
this.token = token; | ||
this.title = title; | ||
this.body = body; | ||
} | ||
} |
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,12 @@ | ||
package com.cmc.suppin.fcm.service; | ||
|
||
import com.cmc.suppin.fcm.controller.dto.FcmSendDTO; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
public interface FcmService { | ||
|
||
int sendMessageTo(FcmSendDTO fcmSendDTO) throws IOException; | ||
} |
Oops, something went wrong.