-
Notifications
You must be signed in to change notification settings - Fork 2
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
[feat]: 푸시알람 구현 #39
Merged
Merged
[feat]: 푸시알람 구현 #39
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67cde01
[feat]: 푸시알람 관련 firebaseConfig 작성
realisshomyang 41cbc61
[feat]: 푸시알람 테스트 api 작성
realisshomyang d9b3af8
[feat]: 푸시알람 수신 시간 컬럼 추가
realisshomyang bb48973
[feat]: 푸시알람 분 단위 전송 기능 추가
realisshomyang dac111e
[fix]: merge pull request
realisshomyang 856d4d6
[fix]: 와이어 프레임에 맞게 푸시 알람 메시지 수정
realisshomyang 216e991
[fix]: merge conflict 해소
realisshomyang 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
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
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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/onnoff/onnoff/config/FirebaseConfig.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,43 @@ | ||
package com.onnoff.onnoff.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Configuration | ||
@Slf4j | ||
public class FirebaseConfig { | ||
private final Logger logger = LoggerFactory.getLogger(FirebaseConfig.class); | ||
@Value("${fcm.firebase-sdk-path}") // your firebase sdk path | ||
private String firebaseSdkPath; | ||
@Value("${fcm.project-id}") | ||
private String projectId; | ||
@PostConstruct | ||
public void initialize() { | ||
try { | ||
ClassPathResource resource = new ClassPathResource(firebaseSdkPath); | ||
InputStream serviceAccount = resource.getInputStream(); | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.build(); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
log.info("Successfully firebase application initialized!"); | ||
} | ||
} catch (FileNotFoundException e) { | ||
logger.error("Firebase ServiceAccountKey FileNotFoundException" + e.getMessage()); | ||
} catch (IOException e) { | ||
logger.error("FirebaseOptions IOException" + e.getMessage()); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/onnoff/onnoff/domain/push/controller/FcmController.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,21 @@ | ||
package com.onnoff.onnoff.domain.push.controller; | ||
|
||
import com.onnoff.onnoff.domain.push.service.FcmSendNotificationService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FcmController { | ||
private final FcmSendNotificationService fcmSendNotificationService; | ||
|
||
@GetMapping("/message/test") | ||
public ResponseEntity pushTest(){ | ||
fcmSendNotificationService.sendTestNotification(); | ||
return ResponseEntity.ok("good"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/onnoff/onnoff/domain/push/dto/FcmServiceDto.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,24 @@ | ||
package com.onnoff.onnoff.domain.push.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class FcmServiceDto { | ||
private String username; | ||
private Long contentId; | ||
private NotificationType type; | ||
private String title; | ||
private String content; | ||
|
||
public static FcmServiceDto of(String username, Long contentId, NotificationType type, String title, String content){ | ||
FcmServiceDto dto = new FcmServiceDto(); | ||
dto.username = username; | ||
dto.contentId = contentId; | ||
dto.type = type; | ||
dto.title = title; | ||
dto.content = content; | ||
return dto; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/onnoff/onnoff/domain/push/dto/NotificationMessage.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,35 @@ | ||
package com.onnoff.onnoff.domain.push.dto; | ||
|
||
import com.google.firebase.messaging.Notification; | ||
import com.onnoff.onnoff.domain.user.User; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record NotificationMessage( | ||
String title, | ||
String message, | ||
NotificationType type | ||
) { | ||
public static NotificationMessage toGoHomeNotification(User user) { | ||
return NotificationMessage.builder() | ||
.title(user.getNickname() + "님, 이제 퇴근하실 시간이에요.") | ||
.message("") | ||
.type(NotificationType.NOTIFY) | ||
.build(); | ||
} | ||
public static NotificationMessage toGoHomeNotificationTest() { | ||
return NotificationMessage.builder() | ||
.title("예진" + "님, 이제 퇴근하실 시간이에요.") | ||
.message("") | ||
.type(NotificationType.NOTIFY) | ||
.build(); | ||
} | ||
|
||
|
||
public Notification toNotification() { | ||
return Notification.builder() | ||
.setTitle(title) | ||
.setBody(message) | ||
.build(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/onnoff/onnoff/domain/push/dto/NotificationType.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,5 @@ | ||
package com.onnoff.onnoff.domain.push.dto; | ||
|
||
public enum NotificationType { | ||
NOTIFY | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/onnoff/onnoff/domain/push/service/FcmSendNotificationService.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,43 @@ | ||
package com.onnoff.onnoff.domain.push.service; | ||
|
||
import com.google.firebase.messaging.Message; | ||
import com.onnoff.onnoff.domain.push.dto.NotificationMessage; | ||
import com.onnoff.onnoff.domain.user.User; | ||
import com.onnoff.onnoff.domain.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class FcmSendNotificationService { | ||
private final FcmService fcmService; | ||
private final UserRepository userRepository; | ||
public void sendTestNotification() { | ||
NotificationMessage notificationMessage = | ||
NotificationMessage.toGoHomeNotificationTest(); | ||
final Message message = | ||
fcmService.createMessage("testtoken", notificationMessage); | ||
fcmService.send(message); | ||
} | ||
//1분 단위로 푸시알람 보내는 함수 | ||
@Scheduled(cron = "0 * * * * *", zone = "Asia/Seoul") | ||
public void sendGoHomeNotificationByMinute() { | ||
LocalDateTime currentTime = LocalDateTime.now(); | ||
LocalTime startTime = currentTime.minusSeconds(30).toLocalTime(); | ||
LocalTime endTime = currentTime.plusSeconds(30).toLocalTime(); | ||
List<User> userList = userRepository.findByPushNotificationTimeBetween(startTime, endTime); | ||
for (User user : userList) { | ||
NotificationMessage notificationMessage = NotificationMessage.toGoHomeNotification(user); | ||
Message message = fcmService.createMessage(user.getFcmToken(), notificationMessage); | ||
fcmService.send(message); | ||
} | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/onnoff/onnoff/domain/push/service/FcmService.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,52 @@ | ||
package com.onnoff.onnoff.domain.push.service; | ||
|
||
import com.google.firebase.messaging.*; | ||
import com.onnoff.onnoff.domain.push.dto.NotificationMessage; | ||
import lombok.Builder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
@Builder | ||
@RequiredArgsConstructor | ||
public class FcmService { | ||
public void send(Message message) { | ||
FirebaseMessaging.getInstance() | ||
.sendAsync(message); | ||
} | ||
public Message createMessage(String deviceToken, NotificationMessage notificationMessage) { | ||
return Message.builder() | ||
.setToken(deviceToken) | ||
.setNotification(notificationMessage.toNotification()) | ||
.putData("title", notificationMessage.title()) | ||
.putData("body", notificationMessage.message()) | ||
.putData("type", notificationMessage.type().name()) | ||
.build(); | ||
} | ||
|
||
//다음 행동 유도가 필요할 때 path를 쓴다..! | ||
public Message createMessage(String deviceToken, NotificationMessage notificationMessage, String path) { | ||
|
||
Notification notification = notificationMessage.toNotification(); | ||
return Message.builder() | ||
.setToken(deviceToken) | ||
.setNotification(notification) | ||
.setApnsConfig( | ||
ApnsConfig.builder() | ||
.setAps( | ||
Aps.builder() | ||
.setSound("default") | ||
.build() | ||
) | ||
.build() | ||
) | ||
.putData("title", notificationMessage.title()) | ||
.putData("body", notificationMessage.message()) | ||
.putData("type", notificationMessage.type().name()) | ||
.putData("path", path) | ||
.putData("sound", "default") | ||
.build(); | ||
} | ||
} |
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
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.
궁금해서 그런데 여기서 가져온 fcmToken은 푸시 알림 동의 했을때 set 되는건가요??
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.
네 온보딩에서 푸시 알람 동의했을 때 프론트 측에서 한 번에 유저 정보 업데이트 api 요청할 때 담아서 보내주고, 이를 기반으로 백이 디비에 업데이트 하는 식으로 로직 이해하고 있습니다 ~