-
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.
Merge pull request #99 from tukcomCD2024/feat#58/set-fcm
- Loading branch information
Showing
16 changed files
with
178 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ out/ | |
.vscode/ | ||
|
||
src/main/resources/application-* | ||
|
||
src/main/resources/firebase/firebase_key.json |
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
34 changes: 34 additions & 0 deletions
34
backend/memetory/src/main/java/com/example/memetory/global/config/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 com.example.memetory.global.config; | ||
|
||
import java.io.IOException; | ||
|
||
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 com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
@Value("${spring.firebase.path}") | ||
private String firebasePath; | ||
|
||
@Bean | ||
public FirebaseApp initializeFirebase() { | ||
try { | ||
FirebaseOptions options = new FirebaseOptions.Builder() | ||
.setCredentials( | ||
GoogleCredentials.fromStream(new ClassPathResource(firebasePath).getInputStream())) | ||
.build(); | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
return FirebaseApp.initializeApp(options); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
return FirebaseApp.getInstance(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
backend/memetory/src/main/java/com/example/memetory/global/firebase/FirebaseMessage.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,32 @@ | ||
package com.example.memetory.global.firebase; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum FirebaseMessage { | ||
MEME_CREATE_MESSAGE("밈 생성 완료", "밈 생성이 완료되었습니다."), | ||
; | ||
|
||
private String title; | ||
private String body; | ||
|
||
public Message toMessageWithFcmToken(String fcmToken) { | ||
return Message.builder() | ||
.setToken(fcmToken) | ||
.setNotification( | ||
Notification.builder() | ||
.setTitle(this.getTitle()) | ||
.setBody(this.getBody()) | ||
.build() | ||
) | ||
.putData("time", LocalDateTime.now().toString()) | ||
.build(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../memetory/src/main/java/com/example/memetory/global/firebase/service/FirebaseService.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.example.memetory.global.firebase.service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.example.memetory.global.firebase.FirebaseMessage; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class FirebaseService { | ||
private final FirebaseApp firebaseApp; | ||
|
||
public void sendMessage(Message message) { | ||
try { | ||
String response = FirebaseMessaging.getInstance(firebaseApp).send(message); | ||
log.info("Sent message: {}", response); | ||
} catch (FirebaseMessagingException e) { | ||
log.error("cannot send message by token. error info : {}", e.getMessage()); | ||
} | ||
} | ||
|
||
private Message buildMessage(FirebaseMessage message, String fcmToken) { | ||
return Message.builder() | ||
.setToken(fcmToken) | ||
.setNotification( | ||
Notification.builder() | ||
.setTitle(message.getTitle()) | ||
.setBody(message.getBody()) | ||
.build() | ||
) | ||
.putData("time", LocalDateTime.now().toString()) | ||
.build(); | ||
} | ||
} |
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
Empty file.
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
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