-
Notifications
You must be signed in to change notification settings - Fork 1
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 #18 from MealTokTok/feature/alarm-sender
[#17] [FEATURE] 푸시 알림 구현
- Loading branch information
Showing
19 changed files
with
145 additions
and
116 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
application/app-api/src/main/java/core/startup/mealtoktok/api/auth/AuthApi.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
5 changes: 2 additions & 3 deletions
5
application/app-api/src/main/java/core/startup/mealtoktok/api/auth/AuthApiDocs.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
2 changes: 1 addition & 1 deletion
2
...ealtoktok/api/dto/OAuthLoginResponse.java → ...ktok/api/auth/dto/OAuthLoginResponse.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
2 changes: 1 addition & 1 deletion
2
...tup/mealtoktok/api/dto/SignupRequest.java → ...ealtoktok/api/auth/dto/SignupRequest.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
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
22 changes: 0 additions & 22 deletions
22
...i/src/main/java/core/startup/mealtoktok/api/global/security/JwtExceptionHandleFilter.java
This file was deleted.
Oops, something went wrong.
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
54 changes: 0 additions & 54 deletions
54
application/app-api/src/main/java/core/startup/mealtoktok/api/global/util/CookieUtils.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
application/app-api/src/main/java/core/startup/mealtoktok/api/global/util/HttpUtils.java
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
domain/src/main/java/core/startup/mealtoktok/domain/alarm/Alarm.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,7 @@ | ||
package core.startup.mealtoktok.domain.alarm; | ||
|
||
public record Alarm ( | ||
String title, | ||
String body | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
domain/src/main/java/core/startup/mealtoktok/domain/alarm/AlarmSender.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,7 @@ | ||
package core.startup.mealtoktok.domain.alarm; | ||
|
||
import core.startup.mealtoktok.domain.user.User; | ||
|
||
public interface AlarmSender { | ||
void send(User user, Alarm alarm); | ||
} |
21 changes: 21 additions & 0 deletions
21
domain/src/main/java/core/startup/mealtoktok/domain/alarm/exception/AlarmErrorCode.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 core.startup.mealtoktok.domain.alarm.exception; | ||
|
||
import core.startup.mealtoktok.common.exception.BaseErrorCode; | ||
import core.startup.mealtoktok.common.exception.ErrorReason; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum AlarmErrorCode implements BaseErrorCode { | ||
ALARM_SEND_FAIL(500, "ALARM_500_1", "알람 전송에 실패했습니다."); | ||
|
||
private final Integer status; | ||
private final String errorCode; | ||
private final String message; | ||
|
||
@Override | ||
public ErrorReason getErrorReason() { | ||
return ErrorReason.of(status, errorCode, message); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
infra/src/main/java/core/startup/mealtoktok/infra/notification/FcmAlarmSender.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,46 @@ | ||
package core.startup.mealtoktok.infra.notification; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.MulticastMessage; | ||
import com.google.firebase.messaging.Notification; | ||
import core.startup.mealtoktok.domain.alarm.Alarm; | ||
import core.startup.mealtoktok.domain.alarm.AlarmSender; | ||
import core.startup.mealtoktok.domain.user.User; | ||
import core.startup.mealtoktok.infra.notification.exception.AlarmSendFailException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FcmAlarmSender implements AlarmSender { | ||
|
||
private final FirebaseMessaging firebaseMessaging; | ||
|
||
@Override | ||
public void send(User user, Alarm alarm) { | ||
MulticastMessage message = makeMessage(user, alarm); | ||
try { | ||
firebaseMessaging.sendEachForMulticast(message); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("userId : {} 님에게 알람 전송을 실패하였습니다.", user.getUserId(), e); | ||
throw AlarmSendFailException.EXCEPTION; | ||
} | ||
} | ||
|
||
private MulticastMessage makeMessage(User user, Alarm alarm) { | ||
Notification notification = Notification.builder() | ||
.setTitle(alarm.title()) | ||
.setBody(alarm.body()) | ||
.build(); | ||
|
||
return MulticastMessage.builder() | ||
.setNotification(notification) | ||
.addAllTokens(user.getDeviceTokens()) | ||
.build(); | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
infra/src/main/java/core/startup/mealtoktok/infra/notification/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,38 @@ | ||
package core.startup.mealtoktok.infra.notification.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
|
||
@Configuration | ||
public class FcmConfig { | ||
|
||
@Value("${fcm.secret-key}") | ||
private String fcmSecretKey; | ||
|
||
@Bean | ||
public FirebaseApp firebaseApp() throws IOException { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(getDecodedCredentials(fcmSecretKey)) | ||
.build(); | ||
return FirebaseApp.initializeApp(options); | ||
} | ||
|
||
@Bean | ||
public FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) { | ||
return FirebaseMessaging.getInstance(firebaseApp); | ||
} | ||
|
||
private GoogleCredentials getDecodedCredentials(String encodedKey) throws IOException { | ||
byte[] decodedKey = Base64.getDecoder().decode(encodedKey); | ||
return GoogleCredentials.fromStream(new ByteArrayInputStream(decodedKey)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ain/java/core/startup/mealtoktok/infra/notification/exception/AlarmSendFailException.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 core.startup.mealtoktok.infra.notification.exception; | ||
|
||
import core.startup.mealtoktok.common.exception.InfraException; | ||
import core.startup.mealtoktok.domain.alarm.exception.AlarmErrorCode; | ||
|
||
public class AlarmSendFailException extends InfraException { | ||
|
||
public static final AlarmSendFailException EXCEPTION = new AlarmSendFailException(); | ||
|
||
private AlarmSendFailException() { | ||
super(AlarmErrorCode.ALARM_SEND_FAIL); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ spring: | |
jpa: | ||
open-in-view: false | ||
|
||
fcm: | ||
secret-key: ${FCM_SECRET_KEY} | ||
--- | ||
spring: | ||
config: | ||
|