Skip to content

Commit

Permalink
feat: #39 notification service 내 send notification 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 24, 2024
1 parent 49f5bc4 commit f047af4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.co.fitapet.infra.client.fcm;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class FcmNotificationServiceImpl implements NotificationService {
private final FirebaseMessaging firebaseMessaging;

@Override
public void sendNotification(NotificationRequest request) throws FirebaseMessagingException {
Message message = Message.builder()
.setToken(request.token())
.setNotification(request.toNotification())
.build();

firebaseMessaging.send(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kr.co.fitapet.infra.client.fcm;

import com.google.firebase.messaging.Notification;
import lombok.Builder;

@Builder
public record NotificationRequest(
String token,
String title,
String body
) {
public NotificationRequest {
if (token == null || token.isBlank()) {
throw new IllegalArgumentException("token must not be null or empty");
}
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("title must not be null or empty");
}
if (body == null || body.isBlank()) {
throw new IllegalArgumentException("body must not be null or empty");
}
}

public static NotificationRequest valueOf(String token, String title, String body) {
return new NotificationRequest(token, title, body);
}

public Notification toNotification() {
return Notification.builder()
.setTitle(title)
.setBody(body)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.co.fitapet.infra.client.fcm;

import com.google.firebase.messaging.FirebaseMessagingException;

public interface NotificationService {
void sendNotification(NotificationRequest request) throws FirebaseMessagingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -12,7 +13,11 @@

@Configuration
public class FcmConfig {
private final ClassPathResource firebaseResource = new ClassPathResource("firebase/fitapet-ios-firebase-adminsdk-ethnn-6ec10fe329.json");
private final ClassPathResource firebaseResource;

public FcmConfig(@Value("${app.firebase.config.file}") String firebaseConfigPath) {
this.firebaseResource = new ClassPathResource(firebaseConfigPath);
}

@Bean
FirebaseApp firebaseApp() throws IOException {
Expand Down
12 changes: 11 additions & 1 deletion fitapet-infra/src/main/resources/application-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ cloud:
stack:
auto: false

app:
firebase:
config:
file: ${FIREBASE_CONFIG_FILE}

---
spring:
config:
Expand Down Expand Up @@ -127,4 +132,9 @@ cloud:
static: ${NCP_OBJECT_STORAGE_REGION}
auto: false
stack:
auto: false
auto: false

app:
firebase:
config:
file: ${FIREBASE_CONFIG_FILE}

0 comments on commit f047af4

Please sign in to comment.