-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1394 from innovationacademy-kr/common/feat_web_pu…
…sh_alarm [COMMON] 웹 푸쉬 알림 기능 테스트용
- Loading branch information
Showing
16 changed files
with
2,069 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,7 @@ out/ | |
application*.yml | ||
!application.yml | ||
|
||
**/static/docs | ||
**/static/docs | ||
|
||
### Firebase ### | ||
*firebase*.json |
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
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/org/ftclub/cabinet/firebase/FCMInitializer.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,36 @@ | ||
package org.ftclub.cabinet.firebase; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.annotation.PostConstruct; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
public class FCMInitializer { | ||
|
||
@Value("${firebase.messaging.credentials.path}") | ||
private String credentialsPath; | ||
|
||
@PostConstruct | ||
public void initialize() throws IOException { | ||
ClassPathResource resource = new ClassPathResource(credentialsPath); | ||
|
||
try (InputStream inputStream = resource.getInputStream()) { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(inputStream)) | ||
.build(); | ||
|
||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
log.info("Firebase application has been initialized"); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/org/ftclub/cabinet/firebase/FCMTestController.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,34 @@ | ||
package org.ftclub.cabinet.firebase; | ||
|
||
import java.time.Duration; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import org.ftclub.cabinet.firebase.fcm.service.FCMService; | ||
import org.ftclub.cabinet.redis.service.RedisService; | ||
import org.ftclub.cabinet.utils.overdue.manager.OverdueType; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/v4/fcm") | ||
@Log4j2 | ||
public class FCMTestController { | ||
private final RedisService redisService; | ||
private final FCMService fcmService; | ||
|
||
@PostMapping("test/{token}") | ||
public void test(@PathVariable("token") String token) { | ||
log.info("called test token = {}", token); | ||
redisService.save("sichoi", token, Duration.ofDays(1)); | ||
} | ||
|
||
@PostMapping("test2/{name}") | ||
public void test2(@PathVariable("name") String name) { | ||
log.info("called test2"); | ||
fcmService.sendPushMessage(name, OverdueType.OVERDUE, 1L); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
backend/src/main/java/org/ftclub/cabinet/firebase/fcm/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,72 @@ | ||
package org.ftclub.cabinet.firebase.fcm.service; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.ftclub.cabinet.redis.service.RedisService; | ||
import org.ftclub.cabinet.utils.overdue.manager.OverdueType; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FCMService { | ||
private final RedisService redisService; | ||
|
||
|
||
public void sendPushMessage(String name, OverdueType overdueType, Long daysLeftFromExpireDate) { | ||
log.info("called sendPushMessage name = {}, overdueType = {}, daysLeftFromExpireDate = {}", name, overdueType, | ||
daysLeftFromExpireDate); | ||
|
||
Optional<String> token = redisService.findByKey(name, String.class); | ||
if (token.isEmpty()) { | ||
log.warn("\"{}\"에 해당하는 디바이스 토큰이 존재하지 않습니다.", name); | ||
return; | ||
} | ||
|
||
switch (overdueType) { | ||
case NONE: | ||
log.warn("overdueType이 NONE입니다. name = {}, overdueType = {}, daysLeftFromExpireDate = {}", name, overdueType, | ||
daysLeftFromExpireDate); | ||
break; | ||
case SOON_OVERDUE: | ||
sendSoonOverdueMessage(token.get(), name, daysLeftFromExpireDate); | ||
break; | ||
case OVERDUE: | ||
sendOverdueMessage(token.get(), name, daysLeftFromExpireDate); | ||
break; | ||
} | ||
} | ||
|
||
private void sendOverdueMessage(String token, String name, Long daysLeftFromExpireDate) { | ||
log.info( | ||
"called sendOverdueMessage token = {}, name = {}, daysLeftFromExpireDate = {}", | ||
token, name, daysLeftFromExpireDate); | ||
Message message = Message.builder() | ||
.putData("title", "<CABI> 연체 알림") | ||
.putData("content", name + "님, 대여한 사물함이 " + Math.abs(daysLeftFromExpireDate) + "일 연체되었습니다.") | ||
.setToken(token) | ||
.build(); | ||
|
||
FirebaseMessaging.getInstance().sendAsync(message); | ||
} | ||
|
||
private void sendSoonOverdueMessage(String token, String name, Long daysLeftFromExpireDate) { | ||
log.info( | ||
"called sendSoonOverdueMessage token = {}, name = {}, daysLeftFromExpireDate = {}", | ||
token, name, daysLeftFromExpireDate); | ||
if (token.isEmpty()) { | ||
log.warn("\"{}\"에 해당하는 디바이스 토큰이 존재하지 않습니다.", name); | ||
return; | ||
} | ||
Message message = Message.builder() | ||
.putData("title", "<CABI> 연체 예정 알림") | ||
.putData("content", "대여한 사물함이 " + daysLeftFromExpireDate + "일 후 연체됩니다.") | ||
.setToken(token) | ||
.build(); | ||
|
||
FirebaseMessaging.getInstance().sendAsync(message); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/org/ftclub/cabinet/redis/config/RedisConfig.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 org.ftclub.cabinet.redis.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.redis.port}") | ||
private int redisPort; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisHost, redisPort); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate() { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
|
||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
|
||
return redisTemplate; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
backend/src/main/java/org/ftclub/cabinet/redis/service/FCMTokenRedisService.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,63 @@ | ||
package org.ftclub.cabinet.redis.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FCMTokenRedisService implements RedisService { | ||
|
||
private static final String KEY_PREFIX = "cabinet-fcm-token:"; | ||
|
||
private final StringRedisTemplate redisTemplate; | ||
private final ObjectMapper objectMapper; | ||
|
||
|
||
/** | ||
* @param key 조회할 키 | ||
* @param type 조회할 값의 타입 | ||
* @return 조회된 값 | ||
*/ | ||
@Override | ||
public <T> Optional<T> findByKey(String key, Class<T> type) { | ||
String serializedValue = redisTemplate.opsForValue().get(KEY_PREFIX + key); | ||
if (serializedValue == null) { | ||
return Optional.empty(); | ||
} | ||
try { | ||
return Optional.of(objectMapper.readValue(serializedValue, type)); | ||
} catch (Exception e) { | ||
log.error("Redis findByKey error", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* @param key 저장할 키 | ||
* @param data 저장할 값 | ||
* @param duration 저장할 기간 | ||
*/ | ||
@Override | ||
public <T> void save(String key, T data, Duration duration) { | ||
try { | ||
String serializedValue = objectMapper.writeValueAsString(data); | ||
redisTemplate.opsForValue().set(KEY_PREFIX + key, serializedValue, duration); | ||
} catch (Exception e) { | ||
log.error("Redis save error", e); | ||
} | ||
} | ||
|
||
/** | ||
* @param key 삭제할 키 | ||
*/ | ||
@Override | ||
public boolean delete(String key) { | ||
return Boolean.TRUE.equals(redisTemplate.delete(KEY_PREFIX + key)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/org/ftclub/cabinet/redis/service/RedisService.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,13 @@ | ||
package org.ftclub.cabinet.redis.service; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
public interface RedisService { | ||
|
||
<T> Optional<T> findByKey(String key, Class<T> type); | ||
|
||
<T> void save(String key, T data, Duration duration); | ||
|
||
boolean delete(String key); | ||
} |
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
Submodule config
updated
from 9f2c2f to 6b3039
Oops, something went wrong.