-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/modernfarmer/server/farmususer/global/config/Firebase/FcmMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...va/modernfarmer/server/farmususer/global/config/Firebase/FirebaseCloudMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |