Skip to content

Commit

Permalink
[feat] 파이어베이스 메시지 보내기
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Nov 17, 2023
1 parent 41e5a15 commit e2b0644
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ dependencies {
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

// firebase sdk
implementation 'com.google.firebase:firebase-admin:6.8.1'
// okhttp
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.2.2'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package modernfarmer.server.farmususer.global.config.Firebase;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@AllArgsConstructor
@Getter
public class FcmMessage {
private boolean validate_only;
private Message message;
@Builder
@AllArgsConstructor
@Getter
public static class Message {

private Notification notification;

private String token;
}
@Builder
@AllArgsConstructor
@Getter
public static class Notification {
private String title;
private String body;
private String image;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package modernfarmer.server.farmususer.global.config.Firebase;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auth.oauth2.GoogleCredentials;
import lombok.RequiredArgsConstructor;
import okhttp3.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class FirebaseCloudMessageService {

private final String API_URL = "https://fcm.googleapis.com/v1/projects/tourcash-13092/messages:send"; // 프론트에서 제공한 url
private final ObjectMapper objectMapper;

public void sendMessageTo(String targetToken, String title, String body) throws IOException {
String message = makeMessage(targetToken, title, body);
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, "application/json; UTF-8")
.build();
Response response = client.newCall(request)
.execute();

System.out.println(response.body().string());

}
private String makeMessage(String targetToken, String title, String body) throws JsonProcessingException {
FcmMessage fcmMessage = FcmMessage.builder()
.message(FcmMessage.Message.builder()
.token(targetToken)
.notification(FcmMessage.Notification.builder()
.title(title)
.body(body)
.image(null)
.build()
).build()
)
.validate_only(false)
.build();
return objectMapper.writeValueAsString(fcmMessage);
}


private String getAccessToken() throws IOException {
String firebaseConfigPath = "firebase/firebase_service_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();
}

}

0 comments on commit e2b0644

Please sign in to comment.