-
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.
* ✨ Feature: FcmToken 도메인 작성 * ✨ Feature: FCM 알림 기능 구현
- Loading branch information
1 parent
a913553
commit b8149f2
Showing
20 changed files
with
286 additions
and
4 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
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
3 changes: 3 additions & 0 deletions
3
moodoodle-api/src/main/java/zzangdol/user/implement/UserCommandService.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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package zzangdol.user.implement; | ||
|
||
import zzangdol.user.domain.User; | ||
import zzangdol.user.presentation.dto.request.PushNotificationRequest; | ||
import zzangdol.user.presentation.dto.request.UserInfoUpdateRequest; | ||
|
||
public interface UserCommandService { | ||
|
||
User updateUserInfo(User user, UserInfoUpdateRequest request); | ||
|
||
User handlePushNotifications(User user, PushNotificationRequest request); | ||
|
||
void withDrawUser(User user); | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
...dle-api/src/main/java/zzangdol/user/presentation/dto/request/PushNotificationRequest.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 zzangdol.user.presentation.dto.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class PushNotificationRequest { | ||
|
||
private Boolean pushNotificationsEnabled; | ||
private String fcmToken; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
moodoodle-domain/src/main/java/zzangdol/notification/dao/FcmTokenRepository.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,12 @@ | ||
package zzangdol.notification.dao; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import zzangdol.notification.domain.FcmToken; | ||
import zzangdol.user.domain.User; | ||
|
||
public interface FcmTokenRepository extends JpaRepository<FcmToken, Long> { | ||
|
||
Optional<FcmToken> findByUserAndToken(User user, String token); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
moodoodle-domain/src/main/java/zzangdol/notification/domain/FcmToken.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 zzangdol.notification.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import zzangdol.user.domain.User; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
public class FcmToken { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String token; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@Builder | ||
public FcmToken(String token, User user) { | ||
this.token = token; | ||
this.user = user; | ||
} | ||
} |
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,21 @@ | ||
dependencies { | ||
implementation project(':moodoodle-domain') | ||
|
||
// spring boot | ||
implementation 'org.springframework.boot:spring-boot-starter' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
// fcm | ||
implementation 'com.google.firebase:firebase-admin:8.0.0' | ||
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.2.2' | ||
|
||
} | ||
|
||
task copyYML(type: Copy) { | ||
copy { | ||
from '../moodoodle-submodule/notification' | ||
include "*.yml" | ||
into 'src/main/resources' | ||
} | ||
} |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
moodoodle-notification/gradle/wrapper/gradle-wrapper.properties
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,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
37 changes: 37 additions & 0 deletions
37
moodoodle-notification/src/main/java/zzangdol/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,37 @@ | ||
package zzangdol; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import zzangdol.notification.dao.FcmTokenRepository; | ||
import zzangdol.notification.domain.FcmToken; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class FCMService { | ||
|
||
private final FcmTokenRepository fcmTokenRepository; | ||
|
||
public void sendNotification(String token, String title, String body) throws Exception { | ||
Message message = Message.builder() | ||
.putData("title", title) | ||
.putData("body", body) | ||
.setToken(token) | ||
.build(); | ||
|
||
String response = FirebaseMessaging.getInstance().send(message); | ||
System.out.println("Successfully sent message: " + response); | ||
} | ||
|
||
public void sendNotificationToAllUsers(String title, String body) throws Exception { | ||
List<FcmToken> tokens = fcmTokenRepository.findAll(); | ||
for (FcmToken token : tokens) { | ||
sendNotification(token.getToken(), title, body); | ||
} | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
moodoodle-notification/src/main/java/zzangdol/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,34 @@ | ||
package zzangdol; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.Base64; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
|
||
@Value("${fcm.key}") | ||
private String fcmJson; | ||
|
||
@PostConstruct | ||
public void initFirebase() { | ||
String base64String = fcmJson; | ||
byte[] decodedBytes = Base64.getDecoder().decode(base64String); | ||
InputStream credentialStream = new ByteArrayInputStream(decodedBytes); | ||
|
||
try { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(credentialStream)) | ||
.build(); | ||
FirebaseApp.initializeApp(options); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
moodoodle-notification/src/main/java/zzangdol/NotificationScheduler.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,23 @@ | ||
package zzangdol; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class NotificationScheduler { | ||
|
||
private final FCMService fcmService; | ||
|
||
@Scheduled(cron = "0 0 21 * * ?") | ||
public void scheduleDailyNotification() { | ||
try { | ||
String title = "Daily Reminder"; | ||
String body = "This is your daily reminder!"; | ||
fcmService.sendNotificationToAllUsers(title, body); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.