-
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.
Browse files
Browse the repository at this point in the history
[FEAT] ์ ํ ํธ์ ์๋ฆผ ๊ตฌํ
- Loading branch information
Showing
16 changed files
with
600 additions
and
12 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/server/capple/config/apns/config/ApnsClientConfig.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,44 @@ | ||
package com.server.capple.config.apns.config; | ||
|
||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.timeout.WriteTimeoutHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import reactor.netty.http.HttpProtocol; | ||
import reactor.netty.http.client.HttpClient; | ||
import reactor.netty.resources.ConnectionProvider; | ||
|
||
import javax.net.ssl.SSLException; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Configuration | ||
public class ApnsClientConfig { | ||
@Bean("apnsSslContext") | ||
public SslContext getApnsSslContext() throws SSLException { | ||
return SslContextBuilder.forClient().protocols("TLSv1.2").build(); // SSL ์ค์ | ||
} | ||
|
||
@Bean("apnsConnectionProvider") | ||
public ConnectionProvider getApnsConnectionProvider() { | ||
return ConnectionProvider.builder("apns") | ||
.maxConnections(10) // ์ต๋ ์ปค๋ต์ ์ | ||
.pendingAcquireMaxCount(-1) // ์ฌ์๋ ํ์ (-1 : ๋ฌดํ๋) | ||
.pendingAcquireTimeout(java.time.Duration.ofSeconds(10)) // ์ปค๋ฅ์ ํ์ ์ฌ์ฉ ๊ฐ๋ฅํ ์ปค๋ฅ์ ์์ ๋์ ๋๊ธฐ ์๊ฐ | ||
.maxIdleTime(java.time.Duration.ofSeconds(5)) // ์ต๋ ์ ํด ์๊ฐ | ||
.maxLifeTime(java.time.Duration.ofSeconds(300)) // ์ต๋ ์๋ช ์๊ฐ | ||
.lifo() // ํ์ ์ ์ถ | ||
.build(); | ||
} | ||
|
||
@Bean("apnsH2HttpClient") | ||
public HttpClient getApnsH2HttpClient(ConnectionProvider apnsConnectionProvider, SslContext apnsSslContext) { | ||
return HttpClient.create(apnsConnectionProvider) // reactor HttpClient ์์ฑ | ||
.keepAlive(true) // keep-alive ํ์ฑํ | ||
.protocol(HttpProtocol.H2) // HTTP/2 ํ์ฑํ | ||
.doOnConnected(connection -> connection.addHandlerLast(new WriteTimeoutHandler(10, TimeUnit.SECONDS))) // ์ฐ๊ธฐ ํ์ ์์ | ||
.responseTimeout(Duration.ofSeconds(10)) // ์๋ต ํ์ ์์ | ||
.secure(sslSpec -> sslSpec.sslContext(apnsSslContext)); // SSL ํ์ฑํ | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/server/capple/config/apns/dto/ApnsClientRequest.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,93 @@ | ||
package com.server.capple.config.apns.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.*; | ||
|
||
public class ApnsClientRequest { | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@ToString | ||
public static class SimplePushBody { | ||
private Aps aps; | ||
|
||
public SimplePushBody(String title, String subTitle, String body, Integer badge, String threadId, String targetContentId) { | ||
this.aps = new Aps(new Aps.Alert(title, subTitle, body), badge, threadId, targetContentId); | ||
} | ||
|
||
@ToString | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class Aps { | ||
private Alert alert; | ||
private Integer badge; | ||
@JsonProperty("thread-id") | ||
private String threadId; | ||
@JsonProperty("target-content-id") | ||
private String targetContentId; // ํ๋ก ํธ ์ธก ์์ ํ์ํจ | ||
|
||
@ToString | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class Alert { | ||
private String title; | ||
private String subtitle; | ||
private String body; | ||
} | ||
} | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@ToString | ||
public static class FullAlertBody { | ||
private Aps aps; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@ToString | ||
public static class Aps { | ||
private Alert alert; // alert ์ ๋ณด | ||
private Integer badge; // ์ฑ ์์ด์ฝ์ ํ์ํ ๋ฑ์ง ์ซ์ | ||
@Schema(defaultValue = "default") | ||
private String sound; // Library/Sounds ํด๋ ๋ด์ ํ์ผ ์ด๋ฆ | ||
@Schema(defaultValue = "thread-id") | ||
private String threadId; // ์๋ฆผ ๊ทธ๋ฃนํ๋ฅผ ์ํ thread id (UNNotificationContent ๊ฐ์ฒด์ threadIdentifier์ ์ผ์นํด์ผ ํจ) | ||
private String category; // ์๋ฆผ ๊ทธ๋ฃนํ๋ฅผ ์ํ category, (UNNotificationCategory ์๋ณ์์ ์ผ์นํด์ผ ํจ) | ||
@Schema(defaultValue = "0") | ||
@JsonProperty("content-available") | ||
private Integer contentAvailable; // ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋ฆผ ์ฌ๋ถ, 1์ด๋ฉด ๋ฐฑ๊ทธ๋ผ์ด๋ ์๋ฆผ, 0์ด๋ฉด ํฌ๊ทธ๋ผ์ด๋ ์๋ฆผ (๋ฐฑ๊ทธ๋ผ์ด๋์ผ ๊ฒฝ์ฐ alert, badge, sound๋ ๋ฃ์ผ๋ฉด ์๋จ) | ||
@Schema(defaultValue = "0") | ||
@JsonProperty("mutable-content") | ||
private Integer mutableContent; // ์๋ฆผ ์๋น์ค ํ์ฅ ํ๋๊ทธ | ||
@Schema(defaultValue = "") | ||
@JsonProperty("target-content-id") | ||
private String targetContentId; // ์๋ฆผ์ด ํด๋ฆญ๋์์ ๋ ๊ฐ์ ธ์ฌ ์ฐฝ์ ์๋ณ์, UNNotificationContent ๊ฐ์ฒด์ ์ฑ์์ง | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@ToString | ||
public static class Alert { | ||
@Schema(defaultValue = "title") | ||
private String title; | ||
@Schema(defaultValue = "subTitle") | ||
private String subtitle; | ||
@Schema(defaultValue = "body") | ||
private String body; | ||
@Schema(defaultValue = "") | ||
@JsonProperty("launch-image") | ||
private String launchImage; // ์คํ์ ๋ณด์ฌ์ค ์ด๋ฏธ์ง ํ์ผ, ๊ธฐ๋ณธ ์คํ ์ด๋ฏธ์ง ๋์ ์ ๋ ฅํ ์ด๋ฏธ์ง ๋๋ ์คํ ๋ฆฌ๋ณด๋๊ฐ ์ผ์ง | ||
} | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/server/capple/config/apns/service/ApnsService.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,10 @@ | ||
package com.server.capple.config.apns.service; | ||
|
||
import java.util.List; | ||
|
||
public interface ApnsService { | ||
<T> Boolean sendApns(T request, String ... deviceTokens); | ||
<T> Boolean sendApns(T request, List<String> deviceTokenList); | ||
<T> Boolean sendApnsToMembers(T request, Long ... memberIds); | ||
<T> Boolean sendApnsToMembers(T request, List<Long> memberIdList); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/com/server/capple/config/apns/service/ApnsServiceImpl.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,101 @@ | ||
package com.server.capple.config.apns.service; | ||
|
||
import com.server.capple.config.security.jwt.service.JwtService; | ||
import com.server.capple.domain.member.repository.DeviceTokenRedisRepository; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.reactive.ReactorClientHttpConnector; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.netty.http.client.HttpClient; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ApnsServiceImpl implements ApnsService { | ||
private final JwtService jwtService; | ||
private final HttpClient apnsH2HttpClient; | ||
private final DeviceTokenRedisRepository deviceTokenRedisRepository; | ||
private WebClient defaultApnsWebClient; | ||
|
||
@Value("${apns.base-url}") | ||
private String apnsBaseUrl; | ||
@Value("${apns.base-sub-url}") | ||
private String apnsBaseSubUrl; | ||
@Value("${apple-auth.client_id}") | ||
private String apnsTopic; | ||
private final String apnsAlertPushType = "alert"; | ||
|
||
@PostConstruct | ||
public void init() { | ||
defaultApnsWebClient = WebClient.builder() | ||
.clientConnector(new ReactorClientHttpConnector(apnsH2HttpClient)) | ||
.baseUrl(apnsBaseUrl) | ||
.defaultHeader("apns-topic", apnsTopic) | ||
.defaultHeader("apns-push-type", apnsAlertPushType) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public <T> Boolean sendApns(T request, String... deviceToken) { | ||
return sendApns(request, List.of(deviceToken)); | ||
} | ||
|
||
@Override | ||
public <T> Boolean sendApns(T request, List<String> deviceToken) { | ||
WebClient tmpWebClient = defaultApnsWebClient.mutate() | ||
.defaultHeader("authorization", "bearer " + jwtService.createApnsJwt()) | ||
.build(); | ||
|
||
WebClient tmpSubWebClient = tmpWebClient.mutate() | ||
.baseUrl(apnsBaseSubUrl) | ||
.build(); | ||
|
||
deviceToken.parallelStream() | ||
.forEach(token -> { | ||
tmpWebClient | ||
.method(HttpMethod.POST) | ||
.uri(token) | ||
.bodyValue(request) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.doOnDiscard(Void.class, response -> {// ๊ฑฐ์ ์ ๋ณด์กฐ ์ฑ๋๋ก ์ฌ์๋ | ||
tmpSubWebClient | ||
.method(HttpMethod.POST) | ||
.uri(token) | ||
.bodyValue(request) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.subscribe(); | ||
log.info("APNs ์ ์ก ๊ฑฐ์ ๋ฐ์"); | ||
}) | ||
.doOnError(e -> { // ์๋ฌ ๋ฐ์ ์ ๋ณด์กฐ ์ฑ๋๋ก ์ฌ์๋ | ||
tmpSubWebClient | ||
.method(HttpMethod.POST) | ||
.uri(token) | ||
.bodyValue(request) | ||
.retrieve() | ||
.bodyToMono(Void.class) | ||
.subscribe(); | ||
log.error("APNs ์ ์ก ์ค ์ค๋ฅ ๋ฐ์", e); | ||
}) | ||
.subscribe(); | ||
}); | ||
return true; | ||
} | ||
|
||
@Override | ||
public <T> Boolean sendApnsToMembers(T request, Long... memberIds) { | ||
return sendApns(request, deviceTokenRedisRepository.getDeviceTokens(List.of(memberIds))); | ||
} | ||
|
||
@Override | ||
public <T> Boolean sendApnsToMembers(T request, List<Long> memberIdList) { | ||
return sendApns(request, deviceTokenRedisRepository.getDeviceTokens(memberIdList)); | ||
} | ||
} |
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.