Skip to content

Commit

Permalink
Merge pull request #141 from Hanaro-trip-together-bank/feature/dues_paid
Browse files Browse the repository at this point in the history
refactor: fcm request 수정
  • Loading branch information
ny2060 authored Jun 7, 2024
2 parents 8d9a9ba + 4898063 commit 6e6ae54
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
compileOnly 'org.projectlombok:lombok'
implementation 'com.google.firebase:firebase-admin:9.2.0'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.2.2'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.fasterxml.jackson.core:jackson-core:2.16.1'
implementation 'com.h2database:h2'
implementation 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import com.hanaro.triptogether.team.domain.Team;
import com.hanaro.triptogether.team.domain.TeamRepository;
import lombok.RequiredArgsConstructor;
import okhttp3.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -45,29 +47,44 @@ private String getAccessToken() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(new ClassPathResource(firebaseConfigPath).getInputStream())
.createScoped(List.of("https://www.googleapis.com/auth/cloud-platform"));


googleCredentials.refreshIfExpired();;
return googleCredentials.getAccessToken().getTokenValue();
}

public void sendMessageTo(FcmSendDto fcmSendDto) throws IOException {
String message = makeMessage(fcmSendDto);

OkHttpClient client = new OkHttpClient();
RestTemplate restTemplate = new RestTemplate();

RequestBody requestBody = RequestBody.create(message, MediaType.get("application/json; charset=utf-8"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + getAccessToken());

Request request = new Request.Builder()
.url(API_URL)
.post(requestBody)
.addHeader(HttpHeaders.AUTHORIZATION,"Bearer "+
getAccessToken())
.addHeader(HttpHeaders.CONTENT_TYPE,"application/json; UTF-8")
.build();
HttpEntity<String> entity = new HttpEntity<>(message, headers);

ResponseEntity<String> response = restTemplate.exchange(API_URL, HttpMethod.POST, entity, String.class);

System.out.println(response.getStatusCode());

Response response = client.newCall(request)
.execute();

System.out.println(response.body().string()+"Asdfasdf");
// 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,MediaType.APPLICATION_JSON_VALUE)
// .build();
//
// Response response = client.newCall(request)
// .execute();
//
// System.out.println(response.body().string()+"Asdfasdf");
}

private String makeMessage(FcmSendDto fcmSendDto) throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public BaseResponse<DuesListResponseDto> getDuesList(@RequestParam("teamIdx") Lo

@PostMapping("/request")
public BaseResponse requestDuesToMember(@RequestBody DuesAlarmRequestDto duesAlarmRequestDto) throws IOException, FirebaseMessagingException {
System.out.println("asdfasdf"+duesAlarmRequestDto.toString()+duesAlarmRequestDto.getMemberInfos().toString());
return firebaseFCMService.notificationAlarm("회비 요청 알림",duesAlarmRequestDto.getDuesAmount().toString(),duesAlarmRequestDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.hanaro.triptogether.member.domain.Member;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

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 6e6ae54

Please sign in to comment.