-
Notifications
You must be signed in to change notification settings - Fork 2
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 #64 from Do-Farming/feat/minjoo/dailyMissionSchedu…
- Loading branch information
Showing
11 changed files
with
168 additions
and
3 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,4 +37,4 @@ out/ | |
.vscode/ | ||
|
||
### yml ### | ||
src/main/resources/*.yml | ||
src/main/resources/* |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/hana/api/firebase/controller/TokenController.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,45 @@ | ||
package com.hana.api.firebase.controller; | ||
|
||
import com.hana.api.auth.Auth; | ||
import com.hana.api.user.entity.User; | ||
import com.hana.api.user.service.UserService; | ||
import com.hana.common.config.BaseResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.*; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@RequestMapping("/register-token") | ||
public class TokenController { | ||
final private UserService userService; | ||
|
||
//사용자의 token을 이용해 push하기 위해 받아옴 user db에 auth를 이용해 저장하기 | ||
@Operation(summary = "토큰 주입") | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "1000", description = "수정성공", content = @Content(schema = @Schema(implementation = BaseResponse.SuccessResult.class))), | ||
@ApiResponse(responseCode = "5000", description = "수정실패", content = @Content(schema = @Schema(implementation = BaseResponse.ErrorResult.class))), | ||
}) | ||
@PostMapping | ||
public BaseResponse.SuccessResult<UUID> registerToken(@RequestBody Map<String, String> request, @Auth String usercode) { | ||
String token = request.get("token"); | ||
log.info("++++++++++++++"); | ||
log.info(token); | ||
UUID code = UUID.fromString(usercode); | ||
User user = userService.findbyUser(code); | ||
if(user != null){ | ||
user.setDeviceId(token); | ||
} | ||
System.out.println("Token registered: " + token); | ||
|
||
return BaseResponse.success(userService.save(user).getUserCode()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/hana/api/firebase/service/NotificationService.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,27 @@ | ||
package com.hana.api.firebase.service; | ||
|
||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class NotificationService { | ||
|
||
public void sendNotification(String token, String title, String body) { | ||
try { | ||
Message message = Message.builder() | ||
.setToken(token) | ||
.putData("title", title) | ||
.putData("body", body) | ||
.build(); | ||
|
||
String response = FirebaseMessaging.getInstance().send(message); | ||
System.out.println("Successfully sent message: " + response); | ||
|
||
} catch (Exception e) { | ||
System.err.println("Error sending FCM message: " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/hana/api/firebase/service/PushNotificationService.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 com.hana.api.firebase.service; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class PushNotificationService { | ||
|
||
private static final String EXPO_PUSH_URL = "https://exp.host/--/api/v2/push/send"; | ||
|
||
public void sendNotificationToDevice(String token, String title, String message) { | ||
sendPushNotification(token, title, message); | ||
} | ||
//해당 service 이용해서 영어만!!! 넣어서 보내면 제대로 push 감 token은 user에서 deviedId 테이블 사용 | ||
//title은 큰 글씨 message는 작은 글씨 | ||
private void sendPushNotification(String token, String title, String message) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
String payload = String.format("{\"to\":\"%s\",\"sound\":\"default\",\"title\":\"%s\",\"body\":\"%s\"}", token, title, message); | ||
restTemplate.postForEntity(EXPO_PUSH_URL, payload, String.class); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.hana.common.config; | ||
|
||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
|
||
@Bean | ||
public FirebaseApp firebaseApp() throws IOException { | ||
// 서비스 계정 키 파일 경로 | ||
String serviceAccountPath = "src/main/resources/firebase-service-account.json"; | ||
// 서비스 계정 키 파일을 읽기 위한 InputStream | ||
FileInputStream serviceAccount = new FileInputStream(serviceAccountPath); | ||
|
||
// FirebaseOptions를 사용하여 FirebaseApp 초기화 | ||
FirebaseOptions options = new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.build(); | ||
|
||
// FirebaseApp이 이미 초기화되어 있는지 확인하고, 초기화되지 않은 경우에만 초기화 | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
return FirebaseApp.initializeApp(options); | ||
} else { | ||
return FirebaseApp.getInstance(); | ||
} | ||
} | ||
} |
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