React Native(Expo) ์ฑ์์ ํธ์ ์๋ฆผ์ ๊ตฌํํ๋ ค๋ฉด
Firebase Cloud Messaging(FCM) ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ์์ ์ ์ธ ๊ตฌ์กฐ๋ค.
์ฌ๊ธฐ์๋ ExpoPushToken์ด ์๋ FCM Token ๊ธฐ๋ฐ์ผ๋ก ํธ์๋ฅผ ๊ตฌํํ๋ ์ ์ฒด ํ๋ฆ์ ์ ๋ฆฌํ๋ค.
1. Firebase ๊ธฐ๋ณธ ์ค์
1) Firebase ํ๋ก์ ํธ ์์ฑ
Firebase | Google's Mobile and Web App Development Platform
๊ฐ๋ฐ์๊ฐ ์ฌ์ฉ์๊ฐ ์ข์ํ ๋งํ ์ฑ๊ณผ ๊ฒ์์ ๋น๋ํ๋๋ก ์ง์ํ๋ Google์ ๋ชจ๋ฐ์ผ ๋ฐ ์น ์ฑ ๊ฐ๋ฐ ํ๋ซํผ์ธ Firebase์ ๋ํด ์์๋ณด์ธ์.
firebase.google.com
Firebase Console → ์ ํ๋ก์ ํธ ์์ฑ
ํ์ํ ๊ธฐ๋ฅ:
- Cloud Messaging
- Firebase Admin SDK (๋ฐฑ์๋)
2) Android ์ค์
Android ์ฑ ์ถ๊ฐ → ํจํค์ง๋ช
์
๋ ฅ
๊ฒฝ๋ก ์์:
com.appname.app
์ดํ ์ ๊ณต๋๋ ํ์ผ์ ๋ค์ด๋ก๋:
google-services.json
Expo(EAS)์์๋ ๋ค์์ฒ๋ผ app.json ๋๋ app.config.js์ ๋ฑ๋กํด์ค๋ค:
{
"expo": {
"android": {
"googleServicesFile": "./google-services.json"
}
}
}
2. FCM Token ๊ฐ์ ธ์ค๊ธฐ (Expo ์ฑ)
Expo Go์์๋ FCM Token์ด ์์ฑ๋์ง ์๊ธฐ ๋๋ฌธ์
๋ฐ๋์ EAS Build๋ก ๋ง๋ ๋
๋ฆฝ ์ฑ์์ ํ
์คํธํด์ผ ํ๋ค.
์ฑ์์ ํ ํฐ์ ๊ฐ์ ธ์ค๋ API:
import * as Notifications from "expo-notifications";
const tokenInfo = await Notifications.getDevicePushTokenAsync();
const fcmToken = tokenInfo.data;
์ด๋ ๊ฒ ๋ฐ์์จ FCM Token์ ๋ฐฑ์๋์ ์ ์ฅํด์ผ ํ๋ค.
3. ๋ฐฑ์๋(Firebase Admin SDK) ์ค์
1) Admin SDK ์ค์น
Firebase Console
→ ํ๋ก์ ํธ ์ค์
→ ์๋น์ค ๊ณ์
→ “์ ๋น๊ณต๊ฐ ํค ์์ฑ”
ํ์ผ๋ช ์:
bimong-firebase-adminsdk-xxxx.json
Spring Boot ๋ฆฌ์์ค ํด๋ ๋๋ Docker ์ปจํ
์ด๋ ๋ด์ ๋ฃ์ ํ
application.properties์์ ๊ฒฝ๋ก ์ค์ :
firebase.service-account=/app/config/firebase-service-account.json
3) FirebaseApp ์ด๊ธฐํ
@Configuration
public class FirebaseConfig {
@Value("${firebase.service-account}")
private String serviceAccountPath;
@PostConstruct
public void init() throws IOException {
FileInputStream serviceAccount = new FileInputStream(serviceAccountPath);
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
}
}
4. ๋ฐฑ์๋์์ FCM์ผ๋ก ํธ์ ๋ณด๋ด๊ธฐ
1) ๋ฉ์์ง DTO
public class PushMessageDTO {
private String token;
private String title;
private String body;
}
2) ๋ฉ์์ง ์ ์ก ์๋น์ค
@Service
public class FcmService {
public void sendMessage(PushMessageDTO dto) throws Exception {
Message message = Message.builder()
.setToken(dto.getToken())
.setNotification(
Notification.builder()
.setTitle(dto.getTitle())
.setBody(dto.getBody())
.build()
)
.build();
String response = FirebaseMessaging.getInstance().send(message);
System.out.println("FCM ์๋ต: " + response);
}
}
FirebaseMessaging์ FCM v1 API ๊ธฐ๋ฐ์ผ๋ก ์๋ํ๋ค.
5. ์ค์ ๋์ ํ๋ฆ
- ์ฑ ์คํ → FCM Token ๋ฐ๊ธ
- ์ฑ์ด ํ ํฐ์ ๋ฐฑ์๋๋ก ์ ์ก
- ๋ฐฑ์๋ DB์ deviceId + fcmToken ์ ์ฅ
- ์ค์ผ์ค๋ฌ๋ ํน์ ๋น์ฆ๋์ค ๋ก์ง์์ fcmToken ์กฐํ
- Firebase Admin SDK → FCM ์๋ฒ์ ๋ฉ์์ง ์ ์ก
- FCM ์๋ฒ → ๊ธฐ๊ธฐ(iOS/Android)๋ก ์๋ฆผ ์ ๋ฌ
- ์ฑ์ด ๋ฐฑ๊ทธ๋ผ์ด๋/๊บผ์ง ์ํ์ฌ๋ ์๋ฆผ ์์
6. ์ ๋ฆฌ
ํธ์๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋ ค๋ฉด:
- Firebase Cloud Messaging ์ค์
- google-services.json / plist ํฌํจ๋ EAS ์ฑ ๋น๋
- ์ฑ์์ FCM Token ๋ฐ๊ธ
- ๋ฐฑ์๋์ ์ ์ฅ
- ๋ฐฑ์๋์์ Firebase Admin SDK๋ก ํธ์
์ด ๊ตฌ์กฐ๊ฐ ์์ฑ๋ผ์ผ ๊ธฐ๊ธฐ์์ ํธ์๊ฐ ์ ์ ๋์ํ๋ค