-
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.
푸시 알림을 위한 FCM 관련 로직 작성 admin-sdk파일 등록 Related to: #59
- Loading branch information
1 parent
a238c17
commit ef5fa48
Showing
6 changed files
with
122 additions
and
1 deletion.
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
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
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,47 @@ | ||
package io.sobok.SobokSobok.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
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; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
@Configuration | ||
public class FCMConfig { | ||
|
||
@Value("${firebase.admin-sdk}") | ||
String adminSdkFileName; | ||
|
||
@Bean | ||
FirebaseMessaging firebaseMessaging() throws IOException { | ||
ClassPathResource resource = new ClassPathResource("firebase/" + adminSdkFileName); | ||
|
||
InputStream refreshToken = resource.getInputStream(); | ||
|
||
FirebaseApp firebaseApp = null; | ||
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(); | ||
|
||
if (firebaseAppList != null && !firebaseAppList.isEmpty()) { | ||
for (FirebaseApp app : firebaseAppList) { | ||
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
firebaseApp = app; | ||
} | ||
} | ||
} else { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(refreshToken)) | ||
.build(); | ||
|
||
firebaseApp = FirebaseApp.initializeApp(options); | ||
} | ||
|
||
return FirebaseMessaging.getInstance(firebaseApp); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/main/java/io/sobok/SobokSobok/external/firebase/FCMNotificationService.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,51 @@ | ||
package io.sobok.SobokSobok.external.firebase; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import io.sobok.SobokSobok.auth.application.util.UserServiceUtil; | ||
import io.sobok.SobokSobok.auth.domain.User; | ||
import io.sobok.SobokSobok.auth.infrastructure.UserRepository; | ||
import io.sobok.SobokSobok.exception.ErrorCode; | ||
import io.sobok.SobokSobok.exception.model.NotFoundException; | ||
import io.sobok.SobokSobok.external.firebase.dto.FCMNotificationRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FCMNotificationService { | ||
|
||
private final FirebaseMessaging firebaseMessaging; | ||
private final UserRepository userRepository; | ||
|
||
public void sendNotificationByDeviceToken(FCMNotificationRequest request) { | ||
|
||
User user = UserServiceUtil.findUserById(userRepository, request.userId()); | ||
|
||
if (user.getDeviceToken() == null) { | ||
throw new NotFoundException(ErrorCode.EMPTY_DEVICE_TOKEN); | ||
} | ||
|
||
Notification notification = Notification.builder() | ||
.setTitle(request.title()) | ||
.setBody(request.body()) | ||
.setImage(request.image()) | ||
.build(); | ||
|
||
Message message = Message.builder() | ||
.setToken(user.getDeviceToken()) | ||
.setNotification(notification) | ||
.putAllData(request.data()) | ||
.build(); | ||
|
||
try { | ||
firebaseMessaging.send(message); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("푸시알림 전송에 실패했습니다. userId: " + user.getId() + "\n" + e.getMessage());; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/sobok/SobokSobok/external/firebase/dto/FCMNotificationRequest.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,16 @@ | ||
package io.sobok.SobokSobok.external.firebase.dto; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.Map; | ||
|
||
@Builder | ||
public record FCMNotificationRequest( | ||
|
||
Long userId, | ||
String title, | ||
String body, | ||
String image, | ||
Map<String, String> data | ||
) { | ||
} |