-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
메세지를 전송할 수 있다 #194
Merged
Merged
메세지를 전송할 수 있다 #194
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4a83394
feat: Fcm 초기설정
Hwanvely 9502ed5
feat: Fcm 메세지 구현
Hwanvely ee3707b
feat: 메세지를 발송할 수 있다
Hwanvely aef4cba
feat: Controller 구현
Hwanvely c68c96d
refactor: Firebase Admin 버전업
Hwanvely 97d10da
refactor: 경로 상수화
Hwanvely ffd3f91
refactor: 클래스 이름 수정 및 삭제
Hwanvely a168814
feat: FCM을 통해 Multicast를 할 수 있다
Hwanvely 2950ed1
test: FCM을 통해 Multicast를 할 수 있다
Hwanvely 83162b4
refactor: firebase 라이브러리 사용하도록 수정
Hwanvely 898d317
feat: 푸시 발송 controller 구현
Hwanvely e4e8545
rename: notification->push 패키지 이름 수정
Hwanvely 7b42c8b
rename: FCMResource 경로 수정
Hwanvely d4836e0
chore: Firebase 시크릿키 추가
Hwanvely 3d73f0d
refactor: 에러메세지 수정
Hwanvely 09121a9
feat: 리뷰 반영
Hwanvely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/snackgame/server/messaging/push/config/FCMConfig.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,37 @@ | ||
package com.snackgame.server.messaging.push.config; | ||
|
||
import java.io.InputStream; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.snackgame.server.messaging.push.exception.FCMException; | ||
|
||
@Configuration | ||
public class FCMConfig { | ||
|
||
private static final String FIREBASE_PATH_RESOURCE = "secrets/firebase-service-key.json"; | ||
private static final String PROJECT_ID = "snackgame"; | ||
|
||
@PostConstruct | ||
public void init() { | ||
try { | ||
InputStream serviceAccount = new ClassPathResource(FIREBASE_PATH_RESOURCE).getInputStream(); | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.setProjectId(PROJECT_ID) | ||
.build(); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} | ||
} catch (Exception exception) { | ||
throw new FCMException(); | ||
} | ||
} | ||
|
||
} |
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
2 changes: 1 addition & 1 deletion
2
...r/messaging/notification/domain/Device.kt → ...me/server/messaging/push/domain/Device.kt
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
2 changes: 1 addition & 1 deletion
2
...g/notification/domain/DeviceRepository.kt → ...messaging/push/domain/DeviceRepository.kt
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
2 changes: 1 addition & 1 deletion
2
...on/exception/DuplicatedDeviceException.kt → ...sh/exception/DuplicatedDeviceException.kt
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/snackgame/server/messaging/push/exception/FCMException.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,11 @@ | ||
package com.snackgame.server.messaging.push.exception; | ||
|
||
import com.snackgame.server.common.exception.Kind; | ||
|
||
public class FCMException extends NotificationException { | ||
|
||
public FCMException() { | ||
super("잘못된 FCM 접근입니다.", Kind.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...cation/exception/NotificationException.kt → ...g/push/exception/NotificationException.kt
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
14 changes: 7 additions & 7 deletions
14
...tification/service/NotificationService.kt → ...r/messaging/push/service/DeviceService.kt
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/snackgame/server/messaging/push/service/FCMPushService.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,40 @@ | ||
package com.snackgame.server.messaging.push.service; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Future; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.MulticastMessage; | ||
import com.snackgame.server.messaging.push.service.dto.DeviceResponse; | ||
import com.snackgame.server.messaging.push.service.dto.NotificationRequest; | ||
|
||
@Service | ||
public class FCMPushService implements PushService { | ||
|
||
private final DeviceService deviceService; | ||
|
||
public FCMPushService(DeviceService deviceService) { | ||
this.deviceService = deviceService; | ||
} | ||
|
||
@Override | ||
public Future<?> sendPushMessage(NotificationRequest request, Long ownerId) { | ||
MulticastMessage multicastMessage = makeMessage(request, ownerId); | ||
return FirebaseMessaging.getInstance().sendEachForMulticastAsync(multicastMessage); | ||
} | ||
|
||
private MulticastMessage makeMessage(NotificationRequest request, Long ownerId) { | ||
|
||
List<DeviceResponse> devicesOf = deviceService.getDevicesOf(ownerId); | ||
|
||
MulticastMessage message = MulticastMessage.builder() | ||
.addAllTokens(devicesOf.stream().map(DeviceResponse::getToken).collect(Collectors.toList())) | ||
.setNotification(request.toNotification()) | ||
.build(); | ||
return message; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/snackgame/server/messaging/push/service/PushService.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,10 @@ | ||
package com.snackgame.server.messaging.push.service; | ||
|
||
import java.util.concurrent.Future; | ||
|
||
import com.snackgame.server.messaging.push.service.dto.NotificationRequest; | ||
|
||
public interface PushService { | ||
|
||
Future<?> sendPushMessage(NotificationRequest request, Long ownerId); | ||
} |
6 changes: 3 additions & 3 deletions
6
...otification/service/dto/DeviceResponse.kt → ...saging/push/service/dto/DeviceResponse.kt
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
2 changes: 1 addition & 1 deletion
2
...ication/service/dto/DeviceTokenRequest.kt → ...ng/push/service/dto/DeviceTokenRequest.kt
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/snackgame/server/messaging/push/service/dto/NotificationRequest.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,20 @@ | ||
package com.snackgame.server.messaging.push.service.dto; | ||
|
||
import com.google.firebase.messaging.Notification; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class NotificationRequest { | ||
|
||
public String title; | ||
public String body; | ||
|
||
public Notification toNotification() { | ||
return Notification.builder() | ||
.setTitle(title) | ||
.setBody(body) | ||
.build(); | ||
} | ||
|
||
} |
Submodule secrets
updated
from 7bf50b to 710daa
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
49 changes: 49 additions & 0 deletions
49
src/test/java/com/snackgame/server/messaging/push/service/FCMPushServiceTest.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,49 @@ | ||
package com.snackgame.server.messaging.push.service; | ||
|
||
import static com.snackgame.server.member.fixture.MemberFixture.정환; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import com.snackgame.server.member.fixture.MemberFixture; | ||
import com.snackgame.server.messaging.push.service.dto.DeviceTokenRequest; | ||
import com.snackgame.server.messaging.push.service.dto.NotificationRequest; | ||
import com.snackgame.server.support.general.ServiceTest; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@Disabled | ||
@ServiceTest | ||
public class FCMPushServiceTest { | ||
|
||
@Autowired | ||
private FCMPushService fcmPushService; | ||
|
||
@Autowired | ||
private DeviceService deviceService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MemberFixture.saveAll(); | ||
} | ||
|
||
@DisplayName("토큰을 통해 푸시알림을 전송할 수 있다") | ||
@Test | ||
void sendMessage() throws ExecutionException, InterruptedException { | ||
deviceService.registerDeviceFor(정환().getId(), | ||
new DeviceTokenRequest( | ||
"cFwP3VYHh0eyoXkyY9MwJr:APA91bHX5YiVXuIvi-pLDqNHcJMhl7hKrqLTC7opFMbzj4CsXrg1wu2ayG_LFVREto678gQdWGUnmBXwKEpEJTfXheX0Fz83xwqDzVrKvXF3H5t07XXU6e-boq8JnZVCbs6NB_VfGRh8")); | ||
|
||
Future<?> future = fcmPushService.sendPushMessage( | ||
new NotificationRequest("테스트", "테스트"), | ||
정환().getId()); | ||
|
||
future.get(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApplicationContext 생명 주기와 맞물리는 ContextRefreshed 이벤트도 있는데요.
PostConstruct는 Bean 생명 주기와 맞물려서 영향 범위가 이 FCMConfig 빈 안으로 한정이 되네요.
오히려 더 좋은 접근 같아요. 하나 배워갑니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사실 초기화 방법에 대해서 고민을 한 후 선택하여
PostConstruct
를 선택한건 아니었지만(아는게 이것뿐.. 😢)저도 덕분에 다양한 방법이 있다는 것을 알게되었네요 감사합니다!