Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/zipdabang/Server into fe…
Browse files Browse the repository at this point in the history
…ature/140
  • Loading branch information
HyoBN committed Sep 30, 2023
2 parents 3187ffe + 98d1175 commit 63fc9f2
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 9 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ dependencies {
implementation "com.querydsl:querydsl-jpa"
implementation "com.querydsl:querydsl-core"
implementation "com.querydsl:querydsl-collections"

//FCM 의존성
implementation 'com.google.firebase:firebase-admin:9.1.1'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version : '4.2.2'

annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package zipdabang.server.FeignClient.Config;

import feign.Logger;
import feign.RequestInterceptor;
import feign.codec.ErrorDecoder;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import zipdabang.server.FeignClient.exception.FeignClientExceptionErrorDecoder;

@RequiredArgsConstructor
public class FCMFeignConfiguration {

@Bean
public RequestInterceptor requestInterceptor(){
return template -> template.header("Content-Type", "application/json;charset=UTF-8");
}

@Bean
public ErrorDecoder errorDecoder() {
return new FeignClientExceptionErrorDecoder();
}

@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zipdabang.server.FeignClient;
package zipdabang.server.FeignClient.Config;

import feign.Logger;
import feign.RequestInterceptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zipdabang.server.FeignClient;
package zipdabang.server.FeignClient.Config;

import feign.Logger;
import feign.RequestInterceptor;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/zipdabang/server/FeignClient/FCMFeignClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zipdabang.server.FeignClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import zipdabang.server.FeignClient.Config.FCMFeignConfiguration;
import zipdabang.server.FeignClient.dto.fcm.FCMResponseDto;
import zipdabang.server.firebase.fcm.dto.FcmAOSMessage;

@FeignClient(name = "FCMFeign", url = "https://fcm.googleapis.com", configuration = FCMFeignConfiguration.class)
@Component
public interface FCMFeignClient {


@PostMapping("/v1/projects/zipdabang-android/messages:send")
FCMResponseDto getFCMResponse(@RequestHeader("Authorization") String token, @RequestBody String fcmAOSMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import zipdabang.server.FeignClient.dto.KakaoSocialUserDto;
import zipdabang.server.FeignClient.Config.KakaoFeignConfiguration;

@FeignClient(name = "KakaoInfoFeignClient", url = "${oauth.kakao.baseUrl}", configuration = KakaoFeignConfiguration.class)
@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package zipdabang.server.FeignClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import zipdabang.server.FeignClient.Config.NaverFeignConfiguration;
import zipdabang.server.sms.dto.SmsRequestDto;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package zipdabang.server.FeignClient.dto.fcm;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class FCMResponseDto {
String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public WebSecurityCustomizer webSecurityCustomizer(){
"/v3/api-docs/**",
"/swagger-ui/**",
"/docs/**","/members/oauth", "/members/oauth/info","/members/new-token","/members/terms","/categories","/members/exist-nickname",
"/members/phone/sms","/members/phone/auth","/members/temp-login","/auto-login","/notices/**"
"/members/phone/sms","/members/phone/auth","/members/temp-login","/auto-login","/notices/**","/fcm"
);
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/zipdabang/server/firebase/fcm/dto/FcmAOSMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package zipdabang.server.firebase.fcm.dto;

import lombok.*;

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FcmAOSMessage {

private boolean validateOnly;
private Message message;

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class Message{
private Data data;
private String token;
}


@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class Data{
private String title;
private String body;
private String targetView;
private String targetPK;
private String targetNotification;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package zipdabang.server.firebase.fcm.service;

import java.io.IOException;

public interface FirebaseService {

void sendMessageTo(String targetToken, String title, String body, String targetView, String targetPK, String targetNotification) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package zipdabang.server.firebase.fcm.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.json.JsonParseException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zipdabang.server.FeignClient.FCMFeignClient;
import zipdabang.server.FeignClient.dto.fcm.FCMResponseDto;
import zipdabang.server.firebase.fcm.dto.FcmAOSMessage;

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

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FirebaseServiceImpl implements FirebaseService{

private String fcmUrl = "https://fcm.googleapis.com/v1/projects/zipdabang-android/messages:send";

private final ObjectMapper objectMapper;

private final FCMFeignClient fcmFeignClient;

Logger logger = LoggerFactory.getLogger(FirebaseServiceImpl.class);

@Override
@Transactional
public void sendMessageTo(String targetToken, String title, String body, String targetView, String targetPK, String targetNotification) throws IOException {
String aosMessage = makeAOSMessage(targetToken, title, body, targetView, targetPK, targetNotification);

FCMResponseDto fcmResponse = fcmFeignClient.getFCMResponse("Bearer " + getAccessToken(),aosMessage);
logger.info("성공? : {}",fcmResponse);
logger.info("보낸 메세지 : {}",aosMessage);
}

private String makeAOSMessage(String targeToken, String title, String body, String targetView, String targetPK,String targetNotification) throws JsonParseException, JsonProcessingException {
FcmAOSMessage fcmMessage = FcmAOSMessage.builder()
.message(
FcmAOSMessage.Message.builder()
.token(targeToken).
data(FcmAOSMessage.Data.builder()
.title(title)
.body(body)
.targetView(targetView)
.targetNotification(targetNotification)
.targetPK(targetPK).build()
).
build()
)
.validateOnly(false).build();
return objectMapper.writeValueAsString(fcmMessage);
}

private String getAccessToken() throws IOException{
String fireBaseConfigPath = "firebase/zipdabang-firebase-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();
}
}
3 changes: 3 additions & 0 deletions src/main/java/zipdabang/server/service/RootService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import zipdabang.server.domain.Report;
import zipdabang.server.domain.inform.Notification;

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

public interface RootService {
Expand All @@ -17,4 +18,6 @@ public interface RootService {
Notification findNotification(Long notificationId);

List<Report> getAllReports();

void testFCMService(String fcmToken) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import zipdabang.server.domain.Report;
import zipdabang.server.domain.inform.Notification;
import zipdabang.server.domain.member.Member;
import zipdabang.server.firebase.fcm.service.FirebaseService;
import zipdabang.server.repository.CategoryRepository;
import zipdabang.server.repository.NotificationRepository;
import zipdabang.server.repository.ReportRepository;
import zipdabang.server.repository.memberRepositories.MemberRepository;
import zipdabang.server.service.RootService;

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

@Service
Expand All @@ -35,6 +37,8 @@ public class RootServiceImpl implements RootService {

private final TokenProvider tokenProvider;

private final FirebaseService firebaseService;

@Override
public List<Category> getAllCategories() {
return categoryRepository.findAll();
Expand Down Expand Up @@ -87,4 +91,15 @@ public Notification findNotification(Long notificationId) {
public List<Report> getAllReports() {
return reportRepository.findAll();
}

@Override
public void testFCMService(String fcmToken) throws IOException
{
String title = "집다방 FCM 테스트";
String body = "되나? 되나? 되나? 되나?";
String targetView = "레시피";
String targetPK = "1";
String targetNotification = "2";
firebaseService.sendMessageTo(fcmToken,title,body,targetView,targetPK,targetNotification);
}
}
15 changes: 11 additions & 4 deletions src/main/java/zipdabang/server/web/controller/RootController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import zipdabang.server.base.Code;
import zipdabang.server.base.ResponseDto;
import zipdabang.server.converter.RootConverter;
Expand All @@ -23,8 +20,10 @@
import zipdabang.server.service.RootService;
import zipdabang.server.validation.annotation.ExistNotification;
import zipdabang.server.web.dto.common.BaseDto;
import zipdabang.server.web.dto.requestDto.RootRequestDto;
import zipdabang.server.web.dto.responseDto.RootResponseDto;

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

@RestController
Expand Down Expand Up @@ -109,4 +108,12 @@ public ResponseDto<RootResponseDto.ReportListDto> showReportList(){
List<Report> allReports = rootService.getAllReports();
return ResponseDto.of(RootConverter.toReportListDto(allReports));
}

@Operation(summary = "FCM 테스트 API", description = "테스트용")
@PostMapping("/fcm")
public ResponseDto<BaseDto> testFCM(@RequestBody RootRequestDto.FCMTestDto fcmToken) throws IOException
{
rootService.testFCMService(fcmToken.getFcmToken());
return ResponseDto.of(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zipdabang.server.web.dto.requestDto;

import lombok.Getter;

public class RootRequestDto {

@Getter
public static class FCMTestDto{
String fcmToken;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ naver-sms:
serviceId: ${NAVER_SMS_SERVICEID}
senderPhone: ${NAVER_SMS_PHONE}


fcm:
url: ${FCM_URL}
project-num: ${FCM_PROJECT_NUMBER}
13 changes: 13 additions & 0 deletions src/main/resources/firebase/zipdabang-firebase-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "zipdabang-android",
"private_key_id": "d12c0f24a9637589b2d4ea148ca257b47c4178c4",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC2Iu1utX6w9ZZZ\nFSpv0HytxvPz37G1UW6HixLy/NRPUJj8LJNQIjK3lnXuL9doiDGWdjuQChuvEcLg\nt8FCF1F1Jq52RwwwdtHRBT8NqbThcLFqXduWQhxhEZ3Dutccf1JhoxWKd/Rw3lTd\nNO2UROrRZfcWHore8nOIFqIHcSY81BiZKWJbq8030tJIvsE1IRXfxKmkqdJw0xnm\n7GCX0qHKqAzc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n8LcRQ6t5T0eWOPNEdijDoSSg8vn\nkDh6mnqPAp0mTYMxD3LSzAuWvHgAqFbX1DaxnDBP+F6YLjZkzL8IQW1E+QcyFL8U\nptPAVE7B8EybPw5m41qQP30hdbuOEUMsvHGgmTUCgYAHIYOqmDc+P0cmBUquvKdg\n7QF0mz8vKMlABoF3FTJFKdQ3IfMObmMgbyP1jQx3ikKLR0rCp30yaxM9FriN+S7H\nYBvOSkFV8UsPzRt1VLZmewlPRaLH6PvXnZYy8f1U/mEskTA9pOZ9Zht3nS/xskds\nXGWzxejv16mFakQ+XUUcDQ==\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-ipk1z@zipdabang-android.iam.gserviceaccount.com",
"client_id": "102672117766991763499",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-ipk1z%40zipdabang-android.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}

0 comments on commit 63fc9f2

Please sign in to comment.