-
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.
[Feat] FCM push alarm
- Loading branch information
Showing
17 changed files
with
247 additions
and
48 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 |
---|---|---|
|
@@ -37,7 +37,14 @@ jobs: | |
echo "${{ secrets.PROPERTIES }}" >> ./application.properties | ||
shell: bash | ||
|
||
|
||
# 환경 변수 설정 (Firebase) | ||
- name: create-json | ||
id: create-json | ||
uses: jsdaniell/[email protected] | ||
with: | ||
name: ${{ secrets.FIREBASE_JSON_FILENAME }} | ||
json: ${{ secrets.FIREBASE_JSON }} | ||
dir: 'src/main/resources/firebase/' | ||
|
||
- name: Build with Gradle | ||
run: | | ||
|
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 |
---|---|---|
|
@@ -38,4 +38,5 @@ out/ | |
|
||
### classpath ### | ||
application.properties | ||
p8_key.txt | ||
p8_key.txt | ||
firebase |
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,42 @@ | ||
package igoMoney.BE.common.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 org.springframework.core.io.ClassPathResource; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
@Configuration | ||
public class FCMConfig { | ||
|
||
@Value("${firebase-json}") | ||
private String firebaseJsonFileName; | ||
|
||
@Bean | ||
FirebaseMessaging init() throws IOException { | ||
ClassPathResource resource = new ClassPathResource("firebase/"+firebaseJsonFileName+".json"); | ||
InputStream refreshToken = resource.getInputStream(); | ||
FirebaseApp firebaseApp = null; | ||
List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(); | ||
if(firebaseAppList != null && !firebaseAppList.isEmpty()){ | ||
for(FirebaseApp app : firebaseAppList) { | ||
if(app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
firebaseApp = app; | ||
} | ||
} | ||
} else { | ||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(refreshToken)) | ||
.build(); | ||
firebaseApp = FirebaseApp.initializeApp(options); | ||
} | ||
return FirebaseMessaging.getInstance(firebaseApp); | ||
} | ||
} |
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,30 @@ | ||
package igoMoney.BE.common.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; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
} |
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
14 changes: 9 additions & 5 deletions
14
src/main/java/igoMoney/BE/dto/request/AuthAppleLoginRequest.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,15 +1,19 @@ | ||
package igoMoney.BE.dto.request; | ||
|
||
//import jakarta.validation.constraints.NotNull; | ||
import lombok.*; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
@Getter | ||
@Setter | ||
public class AuthAppleLoginRequest { | ||
|
||
private String id; | ||
private String email; | ||
private String nickname; | ||
private String picture ; | ||
private String state; | ||
private String code; | ||
private String id_token; | ||
private String user; | ||
private String refresh_token; | ||
private Long userId; | ||
private String FCMToken; | ||
} |
18 changes: 0 additions & 18 deletions
18
src/main/java/igoMoney/BE/dto/request/FromAppleService.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
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,49 @@ | ||
package igoMoney.BE.service; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import igoMoney.BE.domain.Notification; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class FCMTokenService { | ||
|
||
private final StringRedisTemplate tokenRedisTemplate; | ||
|
||
public void sendNotification(Notification notification) { | ||
Long userId = notification.getUser().getId(); | ||
if(!hasKey(userId)){ return;} | ||
String token = getToken(userId); | ||
Message message = Message.builder() | ||
.putData("title", notification.getTitle()) | ||
.putData("content", notification.getMessage()) | ||
.putData("userId", String.valueOf(userId)) | ||
.setToken(token) | ||
.build(); | ||
sendMessage(message); | ||
} | ||
|
||
public void saveToken(Long userId, String FCMToken){ | ||
tokenRedisTemplate.opsForValue() | ||
.set(String.valueOf(userId), FCMToken); | ||
} | ||
|
||
private String getToken(Long userId) { | ||
return tokenRedisTemplate.opsForValue().get(userId); | ||
} | ||
|
||
public void deleteToken(Long userId) { | ||
tokenRedisTemplate.delete(String.valueOf(userId)); | ||
} | ||
|
||
public boolean hasKey(Long userId){ | ||
return tokenRedisTemplate.hasKey(String.valueOf(userId)); | ||
} | ||
|
||
private void sendMessage(Message message) { | ||
FirebaseMessaging.getInstance().sendAsync(message); | ||
} | ||
} |
Oops, something went wrong.