Skip to content

Commit

Permalink
[refac] #49 FcmService 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo0419 committed Jun 3, 2024
1 parent ff5f5d1 commit 7476fb2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 60 deletions.
37 changes: 14 additions & 23 deletions src/main/java/com/telepigeon/server/config/FirebaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,30 @@
import com.google.firebase.messaging.FirebaseMessaging;
import com.telepigeon.server.exception.NotFoundException;
import com.telepigeon.server.exception.code.NotFoundErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
@Configuration
public class FirebaseConfig {
@Bean
FirebaseMessaging firebaseMessaging() {
public FirebaseApp firebaseApp() {
try {
FileInputStream serviceAccount =
new FileInputStream("src/main/resources/firebase.json");
FirebaseApp firebaseApp = null;
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps();
if (firebaseAppList != null && !firebaseAppList.isEmpty()) {
for(FirebaseApp app : firebaseAppList){
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)){
firebaseApp = app;
}
}
} else {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
firebaseApp = FirebaseApp.initializeApp(options);
}
return FirebaseMessaging.getInstance(firebaseApp);
} catch (IOException e){
throw new NotFoundException(NotFoundErrorCode.NOT_FOUND_FILE);
FileInputStream aboutFirebaseFile = new FileInputStream("src/main/resources/firebase.json");
FirebaseOptions options = FirebaseOptions
.builder()
.setCredentials(GoogleCredentials.fromStream(aboutFirebaseFile))
.build();
return FirebaseApp.initializeApp(options);
} catch (IOException e) {
throw new NotFoundException(NotFoundErrorCode.FIREBASE_JSON_NOT_FOUND);
}
}

@Bean
public FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) {
return FirebaseMessaging.getInstance(firebaseApp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum NotFoundErrorCode implements DefaultErrorCode{
ANSWER_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 답장입니다."),
WORRY_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 걱정입니다"),
REFRESH_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 리프레시 토큰입니다."),
FIREBASE_JSON_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 FIREBASE JSON입니다."),
;

@JsonIgnore
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/telepigeon/server/service/fcm/FcmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

import com.google.firebase.messaging.*;
import com.telepigeon.server.dto.fcm.FcmMessageDto;
import com.telepigeon.server.exception.BusinessException;
import com.telepigeon.server.exception.code.BusinessErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@RequiredArgsConstructor
public class FcmService {
private final FirebaseMessaging firebaseMessaging;

public String sendByToken(final Message message){

public void send(
final String fcmToken,
final FcmMessageDto fcmMessageDto
){
Message message = createMessage(fcmToken, fcmMessageDto);
try{
String response = firebaseMessaging.send(message);
log.info("FcmResponse : " + response);
return response;
firebaseMessaging.send(message);
} catch (FirebaseMessagingException e){
log.info("FcmExcept : " + e.getMessage());
return "fail";
throw new BusinessException(BusinessErrorCode.FCM_SERVER_ERROR);
}
}

Expand Down
31 changes: 0 additions & 31 deletions src/test/java/com/telepigeon/server/fcmTest/FcmServiceTest.java

This file was deleted.

0 comments on commit 7476fb2

Please sign in to comment.