Skip to content

Commit

Permalink
Merge pull request #142 from Hanaro-trip-together-bank/feature/dues_paid
Browse files Browse the repository at this point in the history
refactor: fcm 수정
  • Loading branch information
ny2060 authored Jun 7, 2024
2 parents 6e6ae54 + 0e63e83 commit 76ac6e9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.hanaro.triptogether.common.firebase;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Configuration
public class FirebaseConfig {
@Bean
public FirebaseApp firebaseApp() throws IOException {
InputStream refreshToken = new ClassPathResource("triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream();

FirebaseOptions options = FirebaseOptions
.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.build();
return FirebaseApp.initializeApp(options);
}

@Bean
public FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) {
return FirebaseMessaging.getInstance(firebaseApp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,40 +108,40 @@ private String makeMessage(FcmSendDto fcmSendDto) throws JsonProcessingException
}


private void firebaseCreateOption() throws IOException {
try {
InputStream refreshToken = new ClassPathResource(firebaseConfigPath).getInputStream();

FirebaseApp firebaseApp = null;
List firebaseApps = FirebaseApp.getApps();

if(firebaseApps != null && !firebaseApps.isEmpty()){

for(Object app : firebaseApps){
if (app instanceof FirebaseApp) {
FirebaseApp apps = (FirebaseApp) app;
if (apps.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
firebaseApp = (FirebaseApp) app;
}
}
}

}else{
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken)).build();

FirebaseApp.initializeApp(options);
}


} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}

}
// private void firebaseCreateOption() throws IOException {
// try {
// InputStream refreshToken = new ClassPathResource(firebaseConfigPath).getInputStream();
//
// FirebaseApp firebaseApp = null;
// List firebaseApps = FirebaseApp.getApps();
//
// if(firebaseApps != null && !firebaseApps.isEmpty()){
//
// for(Object app : firebaseApps){
// if (app instanceof FirebaseApp) {
// FirebaseApp apps = (FirebaseApp) app;
// if (apps.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
// firebaseApp = (FirebaseApp) app;
// }
// }
// }
//
// }else{
// FirebaseOptions options = FirebaseOptions.builder()
// .setCredentials(GoogleCredentials.fromStream(refreshToken)).build();
//
// FirebaseApp.initializeApp(options);
// }
//
//
// } catch (IOException e) {
// throw new RuntimeException(e.getMessage());
// }
//
// }
public BaseResponse notificationAlarm(String title, String body, DuesAlarmRequestDto duesAlarmRequestDto) throws IOException, FirebaseMessagingException {

firebaseCreateOption();
//firebaseCreateOption();

List<String> tokenList = new ArrayList<>();
for (DuesAlarmRequestDto.RequestMemberInfo memberInfo:duesAlarmRequestDto.getMemberInfos()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public ResponseEntity<ExceptionResponse> exceptionHandler(final ApiException e)
.build());
}

// @ExceptionHandler(Exception.class)
// public ResponseEntity<BaseResponse> handleAllExceptions(Exception ex) {
// BaseResponse response = BaseResponse.res(ResponseStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
// return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
// }
@ExceptionHandler(Exception.class)
public ResponseEntity<BaseResponse> handleAllExceptions(Exception ex) {
BaseResponse response = BaseResponse.res(ResponseStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<BaseResponse> handleBadRequestException(BadRequestException ex) {
Expand Down

0 comments on commit 76ac6e9

Please sign in to comment.