Skip to content

Commit 1a0b1ac

Browse files
committed
[REMOVE] 푸시 알림 기능 주석 제거
1 parent 8a08dcc commit 1a0b1ac

File tree

4 files changed

+138
-136
lines changed

4 files changed

+138
-136
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
//package org.routineade.RoutineAdeServer.config.firebase;
2-
//
3-
//import com.google.auth.oauth2.GoogleCredentials;
4-
//import com.google.firebase.FirebaseApp;
5-
//import com.google.firebase.FirebaseOptions;
6-
//import com.google.firebase.messaging.FirebaseMessaging;
7-
//import java.io.IOException;
8-
//import java.io.InputStream;
9-
//import java.util.List;
10-
//import org.springframework.beans.factory.annotation.Value;
11-
//import org.springframework.context.annotation.Bean;
12-
//import org.springframework.context.annotation.Configuration;
13-
//import org.springframework.core.io.ClassPathResource;
14-
//
15-
//@Configuration
16-
//public class FCMConfig {
17-
//
18-
// @Value("${firebase.url}")
19-
// String FIRE_BASE_FILE_URL;
20-
//
21-
// @Bean
22-
// FirebaseMessaging firebaseMessaging() throws IOException {
23-
// ClassPathResource resource = new ClassPathResource(FIRE_BASE_FILE_URL);
24-
//
25-
// InputStream refreshToken = resource.getInputStream();
26-
//
27-
// FirebaseApp firebaseApp = null;
28-
// List<FirebaseApp> firebaseAppList = FirebaseApp.getApps();
29-
//
30-
// if (firebaseAppList != null && !firebaseAppList.isEmpty()) {
31-
// for (FirebaseApp app : firebaseAppList) {
32-
// if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
33-
// firebaseApp = app;
34-
// }
35-
// }
36-
// } else {
37-
// FirebaseOptions options = FirebaseOptions.builder()
38-
// .setCredentials(GoogleCredentials.fromStream(refreshToken))
39-
// .build();
40-
// firebaseApp = FirebaseApp.initializeApp(options);
41-
// }
42-
//
43-
// return FirebaseMessaging.getInstance(firebaseApp);
44-
// }
45-
//}
1+
package org.routineade.RoutineAdeServer.config.firebase;
2+
3+
import com.google.auth.oauth2.GoogleCredentials;
4+
import com.google.firebase.FirebaseApp;
5+
import com.google.firebase.FirebaseOptions;
6+
import com.google.firebase.messaging.FirebaseMessaging;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.List;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.io.ClassPathResource;
14+
15+
@Configuration
16+
public class FCMConfig {
17+
18+
@Value("${firebase.url}")
19+
String FIRE_BASE_FILE_URL;
20+
21+
@Bean
22+
FirebaseMessaging firebaseMessaging() throws IOException {
23+
ClassPathResource resource = new ClassPathResource(FIRE_BASE_FILE_URL);
24+
25+
InputStream refreshToken = resource.getInputStream();
26+
27+
FirebaseApp firebaseApp = null;
28+
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps();
29+
30+
if (firebaseAppList != null && !firebaseAppList.isEmpty()) {
31+
for (FirebaseApp app : firebaseAppList) {
32+
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) {
33+
firebaseApp = app;
34+
}
35+
}
36+
} else {
37+
FirebaseOptions options = FirebaseOptions.builder()
38+
.setCredentials(GoogleCredentials.fromStream(refreshToken))
39+
.build();
40+
firebaseApp = FirebaseApp.initializeApp(options);
41+
}
42+
43+
return FirebaseMessaging.getInstance(firebaseApp);
44+
}
45+
}

src/main/java/org/routineade/RoutineAdeServer/controller/UserController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ResponseEntity<Map<String, String>> login(@RequestBody String userId) {
6868

6969
@Operation(summary = "카카오 로그인", description = "카카오 로그인 API", hidden = true)
7070
@GetMapping("/login/kakao")
71-
public ResponseEntity<String> kakaoLogin(@RequestParam("code") String code) throws URISyntaxException {
71+
public ResponseEntity<Void> kakaoLogin(@RequestParam("code") String code) throws URISyntaxException {
7272
HttpHeaders httpHeaders = new HttpHeaders();
7373
String token = kakaoService.login(code);
7474
httpHeaders.setLocation(
@@ -77,7 +77,7 @@ public ResponseEntity<String> kakaoLogin(@RequestParam("code") String code) thro
7777
return ResponseEntity
7878
.status(FOUND)
7979
.headers(httpHeaders)
80-
.body(token);
80+
.build();
8181
}
8282

8383
@Operation(summary = "유저 기본 정보 등록", description = "사용자가 첫 가입 시 기본 정보를 등록하는 API")
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
//package org.routineade.RoutineAdeServer.service;
2-
//
3-
//import com.google.firebase.messaging.FirebaseMessaging;
4-
//import com.google.firebase.messaging.FirebaseMessagingException;
5-
//import com.google.firebase.messaging.Message;
6-
//import com.google.firebase.messaging.Notification;
7-
//import lombok.RequiredArgsConstructor;
8-
//import org.routineade.RoutineAdeServer.domain.User;
9-
//import org.routineade.RoutineAdeServer.dto.firebase.FCMNotificationRequest;
10-
//import org.springframework.stereotype.Service;
11-
//
12-
//@RequiredArgsConstructor
13-
//@Service
14-
//public class FCMNotificationService {
15-
//
16-
// private final FirebaseMessaging firebaseMessaging;
17-
//
18-
// public void sendNotificationByToken(FCMNotificationRequest request) {
19-
// User user = request.user();
20-
//
21-
// if (user.getFirebaseToken() != null) {
22-
// Notification notification = Notification.builder()
23-
// .setTitle(request.title())
24-
// .setBody(request.body())
25-
// .build();
26-
//
27-
// Message message = Message.builder()
28-
// .setToken(user.getFirebaseToken())
29-
// .setNotification(notification)
30-
// .build();
31-
//
32-
// try {
33-
// firebaseMessaging.send(message);
34-
// } catch (FirebaseMessagingException e) {
35-
// throw new IllegalArgumentException("푸시 알림 보내기를 실패했습니다.");
36-
// }
37-
// } else {
38-
// throw new IllegalArgumentException("서버에 저장된 해당 유저의 Firebase Token이 존재하지 않습니다.");
39-
// }
40-
// }
41-
//
42-
//}
1+
package org.routineade.RoutineAdeServer.service;
2+
3+
import com.google.firebase.messaging.FirebaseMessaging;
4+
import com.google.firebase.messaging.FirebaseMessagingException;
5+
import com.google.firebase.messaging.Message;
6+
import com.google.firebase.messaging.Notification;
7+
import lombok.RequiredArgsConstructor;
8+
import org.routineade.RoutineAdeServer.domain.User;
9+
import org.routineade.RoutineAdeServer.dto.firebase.FCMNotificationRequest;
10+
import org.springframework.stereotype.Service;
11+
12+
@RequiredArgsConstructor
13+
@Service
14+
public class FCMNotificationService {
15+
16+
private final FirebaseMessaging firebaseMessaging;
17+
18+
public void sendNotificationByToken(FCMNotificationRequest request) {
19+
User user = request.user();
20+
21+
if (user.getFirebaseToken() != null) {
22+
Notification notification = Notification.builder()
23+
.setTitle(request.title())
24+
.setBody(request.body())
25+
.build();
26+
27+
Message message = Message.builder()
28+
.setToken(user.getFirebaseToken())
29+
.setNotification(notification)
30+
.build();
31+
32+
try {
33+
firebaseMessaging.send(message);
34+
} catch (FirebaseMessagingException e) {
35+
throw new IllegalArgumentException("푸시 알림 보내기를 실패했습니다.");
36+
}
37+
} else {
38+
throw new IllegalArgumentException("서버에 저장된 해당 유저의 Firebase Token이 존재하지 않습니다.");
39+
}
40+
}
41+
42+
}

src/main/java/org/routineade/RoutineAdeServer/service/UserService.java

+49-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.routineade.RoutineAdeServer.service;
22

3+
import java.time.LocalDate;
34
import java.time.YearMonth;
45
import java.time.format.DateTimeFormatter;
56
import java.util.Arrays;
@@ -15,16 +16,23 @@
1516
import org.routineade.RoutineAdeServer.domain.Routine;
1617
import org.routineade.RoutineAdeServer.domain.User;
1718
import org.routineade.RoutineAdeServer.domain.common.Category;
19+
import org.routineade.RoutineAdeServer.dto.firebase.FCMNotificationRequest;
1820
import org.routineade.RoutineAdeServer.dto.firebase.UserFirebeseTokenSaveRequest;
21+
import org.routineade.RoutineAdeServer.dto.routine.GroupRoutineInfo;
22+
import org.routineade.RoutineAdeServer.dto.routine.GroupRoutinesGetResponse;
23+
import org.routineade.RoutineAdeServer.dto.routine.PersonalRoutineInfo;
1924
import org.routineade.RoutineAdeServer.dto.routine.RoutineCategoryStatisticsInfo;
2025
import org.routineade.RoutineAdeServer.dto.routine.RoutinesByUserProfileGetResponse;
26+
import org.routineade.RoutineAdeServer.dto.routine.RoutinesGetResponse;
2127
import org.routineade.RoutineAdeServer.dto.user.UserInfosGetResponse;
2228
import org.routineade.RoutineAdeServer.dto.user.UserRoutineCalenderStatisticsGetResponse;
2329
import org.routineade.RoutineAdeServer.dto.user.UserRoutineCategoryStatisticsGetResponse;
2430
import org.routineade.RoutineAdeServer.dto.user.UserRoutineCompletionStatistics;
31+
import org.routineade.RoutineAdeServer.repository.GroupMemberRepository;
2532
import org.routineade.RoutineAdeServer.repository.GroupRepository;
2633
import org.routineade.RoutineAdeServer.repository.GroupRoutineRepository;
2734
import org.routineade.RoutineAdeServer.repository.UserRepository;
35+
import org.springframework.scheduling.annotation.Scheduled;
2836
import org.springframework.stereotype.Service;
2937
import org.springframework.transaction.annotation.Transactional;
3038
import org.springframework.web.multipart.MultipartFile;
@@ -40,6 +48,8 @@ public class UserService {
4048
private final S3Service s3Service;
4149
private final GroupRepository groupRepository;
4250
private final GroupRoutineRepository groupRoutineRepository;
51+
private final FCMNotificationService fcmNotificationService;
52+
private final GroupMemberRepository groupMemberRepository;
4353
private static final String BASIC_PROFILE_IMAGE = "https://routineade-ducket.s3.ap-northeast-2.amazonaws.com/BasicProfile.png";
4454

4555
@Transactional(readOnly = true)
@@ -168,52 +178,44 @@ private String saveAndGetImage(MultipartFile image) {
168178
return s3Service.getFileURLFromS3(uploadName).toString();
169179
}
170180

171-
// @Scheduled(cron = "0 0 10 * * *")
172-
// private void sendRoutineAlarm() {
173-
// List<User> users = userRepository.findAll();
174-
//
175-
// for (User user : users) {
176-
// RoutinesGetResponse routines = routineService.getRoutines(user,
177-
// LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")));
178-
//
179-
// if (routines.personalRoutines().isEmpty() && routines.groupRoutines().isEmpty()) {
180-
// continue;
181-
// }
182-
//
183-
// List<PersonalRoutineInfo> personalRoutineInfos = routines.personalRoutines().stream()
184-
// .flatMap(personalRoutine -> personalRoutine.routines().stream())
185-
// .toList();
186-
//
187-
// for (PersonalRoutineInfo pRoutine : personalRoutineInfos) {
188-
// String routineTitle = pRoutine.routineTitle();
189-
//
190-
// // 내용 수정
191-
// FCMNotificationRequest request = FCMNotificationRequest.of(user, "", "");
192-
//
193-
// fcmNotificationService.sendNotificationByToken(request);
194-
// }
195-
//
196-
// for (GroupRoutinesGetResponse groupRoutine : routines.groupRoutines()) {
197-
// Group group = groupRepository.findById(groupRoutine.groupId())
198-
// .orElseThrow(() -> new IllegalArgumentException("해당 ID의 그룹이 존재하지 않습니다."));
199-
// if (groupMemberRepository.findByGroupAndUser(group, user)
200-
// .orElseThrow(() -> new IllegalArgumentException("해당 유저가 해당 그룹의 멤버가 아닙니다!"))
201-
// .getIsGroupAlarmEnabled()) {
202-
// List<GroupRoutineInfo> gRoutines = groupRoutine.groupRoutines().stream()
203-
// .flatMap(gr -> gr.routines().stream())
204-
// .toList();
205-
//
206-
// for (GroupRoutineInfo gRoutine : gRoutines) {
207-
// String routineTitle = gRoutine.routineTitle();
208-
//
209-
// // 내용 수정
210-
// FCMNotificationRequest request = FCMNotificationRequest.of(user, "", "");
211-
//
212-
// fcmNotificationService.sendNotificationByToken(request);
213-
// }
214-
// }
215-
// }
216-
// }
217-
// }
181+
@Scheduled(cron = "0 0 10 * * *")
182+
public void sendRoutineAlarm() {
183+
List<User> users = userRepository.findAll();
184+
185+
for (User user : users) {
186+
RoutinesGetResponse routines = routineService.getRoutines(user,
187+
LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")));
188+
if (routines.personalRoutines().isEmpty() && routines.groupRoutines().isEmpty()) {
189+
continue;
190+
}
191+
List<PersonalRoutineInfo> personalRoutineInfos = routines.personalRoutines().stream()
192+
.flatMap(personalRoutine -> personalRoutine.routines().stream())
193+
.filter(PersonalRoutineInfo::isAlarmEnabled)
194+
.toList();
195+
196+
for (PersonalRoutineInfo pRoutine : personalRoutineInfos) {
197+
fcmNotificationService.sendNotificationByToken(
198+
FCMNotificationRequest.of(user, "루틴에이드", pRoutine.routineTitle() + " 할 시간이에요!"));
199+
}
200+
201+
for (GroupRoutinesGetResponse groupRoutine : routines.groupRoutines()) {
202+
Group group = groupRepository.findById(groupRoutine.groupId())
203+
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 그룹이 존재하지 않습니다."));
204+
205+
if (groupMemberRepository.findByGroupAndUser(group, user)
206+
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 해당 그룹의 멤버가 아닙니다!"))
207+
.getIsGroupAlarmEnabled()) {
208+
List<GroupRoutineInfo> gRoutines = groupRoutine.groupRoutines().stream()
209+
.flatMap(gr -> gr.routines().stream())
210+
.toList();
211+
212+
for (GroupRoutineInfo gRoutine : gRoutines) {
213+
fcmNotificationService.sendNotificationByToken(FCMNotificationRequest.of(user, "루틴에이드",
214+
gRoutine.routineTitle() + " 할 시간이에요!"));
215+
}
216+
}
217+
}
218+
}
219+
}
218220

219221
}

0 commit comments

Comments
 (0)