์งํ ์ค์ธ ํ ์ด ํ๋ก์ ํธ์์ ํธ์์๋ฆผ์ ์ฌ์ฉํด์ผ๋๋ค. ์ด์ ์ Firebase ์ค์๊ฐ DB์ ํ ํฐ์ ์ ์ฅํด์ ๊ทธ๊ฑธ ์ฝ์ด๋ค์ฌ ํธ์์๋ฆผ์ ๊ฐ๊ฐ์ธ์๊ฒ ๋ณด๋ด๋ ์์ ์ ์งํํ๋ ค๊ณ ํ๋๋ฐ topic์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๊ฒ๋๋ค.
firebase cli ์ธํ ํ๋ ๋ถ๋ถ์ ๊ณต์๋ฌธ์์ ์ ๋์์์ผ๋ ๋์ด๊ฐ๊ฒ ๋ค.
topic์ ๊ธฐ์ค์ผ๋ก ๋ฉ์์งํ๊ธฐ๋ก ๊ฒฐ์ ํ ์ด์ ๋ ํจ์จ์ฑ ๋๋ฌธ์ด๋ค. ๋ชจ์๊ด๋ฆฌ ์ฑ์ ๋ง๋ค๊ณ ์๋ ์ค์ด๊ณ , ์ด๋ค ์ฌ์ฉ์๊ฐ ๋ชจ์์ ์ฐธ์ฌํ๊ฒ ๋๋ฉด ์ด๋ฏธ ๋ชจ์์ ์ฐธ์ฌ์ค์ด๋ ์ฌ๋๋ค์๊ฒ ์๋ก์ด ์ฐธ์ฌ์๊ฐ ์๊ฒผ๋ค๊ณ ํธ์์๋ฆผ์ผ๋ก ๋ณด๋ด๋๋ก ํ๋ คํ๋ค. ๊ทธ๋์ ์ฒ์์๋ ๋ชจ์์ ์๋ ์ฌ๋๋ค์ ํ ํฐ์ ๊ด๋ฆฌํ๋ collection์ ํ๋ ๋ง๋ค์ด์ ๊ทธ๊ฑธ๋ก ์ํํ๋ฉด์ ๊ฐ ํ ํฐ์ fcm์ ๋ณด๋ด๋ ค๊ณ ํ๋ค๊ฐ topic์ ์ฌ์ฉํ๋ฉด ๋ ํจ์จ์ ์ธ ๋ฉ์์ง์ด ๊ฐ๋ฅํ ๊ฒ์ ์๊ฒ ๋๋ค. ๊ณต์๋ฌธ์์ ๋ณด๋ฉด ์๋์ ๊ฐ์ด ๋์์๋๋ฐ,
ํผ๋๋ฐฑ์ด ๋นจ๋ผ์ผํ๋ ํธ์์๋ฆผ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ด๊ฑธ ์ฌ์ฉํด๋ ๋๋ค๊ณ ํ๋จํ๋ค.
function์ v2๊ฐ ์๋๋ผ v1์ ์ฌ์ฉํ๋ค.(๋ง์ด๊ทธ๋ ์ด์ ๊ฒฝํ๋ ํด๋ณด๊ธฐ ์ํด์)
const functions = require('firebase-functions/v1');
const admin = require("firebase-admin");
admin.initializeApp();
exports.detectMeetingJoinWithFCM = functions.firestore
.document("/DETAILMEETING/{documentId}")
.onUpdate((change, context) => {
const documentId = context.params.documentId;
// ๋ฌธ์๊ฐ ์ญ์ ๋ ๊ฒฝ์ฐ๋ ์ฒ๋ฆฌํ์ง ์์
if (!change.after.exists) {
functions.logger.log("Document deleted", documentId);
return null;
}
const newValue = change.after.data();
const previousValue = change.before.data();
// ๋ฌธ์๊ฐ ์
๋ฐ์ดํธ๋ ๊ฒฝ์ฐ
if (newValue.participants !== previousValue?.participants) {
const participants = newValue.participants;
const meetingContent = newValue.meeting;
functions.logger.log("Detected write", documentId, participants);
const topic = documentId;
const message = {
notification: {
title: `${meetingContent}`,
body: `์ด์ ๋ชจ์์ ์ฐธ๊ฐ์๊ฐ ${participants.length}๋ช
์ด ๋์์ต๋๋ค`
},
topic: topic
};
return admin.messaging().send(message)
.then(response => {
console.log('Successfully sent message:', response);
return null;
})
.catch(error => {
console.error('Error sending message:', error);
return null;
});
}
return null;
});
์ ์ฒด ์ฝ๋๋ค. trigger ๋ถ๋ถ๊ณผ fcm ๋ถ๋ถ ์ด๋ ๊ฒ ๋ ๊ฐ๋ก ๋๋ ์ ์ดํด๋ณด์.
# Trigger
exports.detectMeetingJoinWithFCM = functions.firestore
.document("/DETAILMEETING/{documentId}")
.onUpdate((change, context) => {
const documentId = context.params.documentId;
// ๋ฌธ์๊ฐ ์ญ์ ๋ ๊ฒฝ์ฐ๋ ์ฒ๋ฆฌํ์ง ์์
if (!change.after.exists) {
functions.logger.log("Document deleted", documentId);
return null;
}
const newValue = change.after.data();
const previousValue = change.before.data();
// ๋ฌธ์๊ฐ ์
๋ฐ์ดํธ๋ ๊ฒฝ์ฐ
if (newValue.participants !== previousValue?.participants) {
const participants = newValue.participants;
functions.logger.log("Detected write", documentId, participants);
}
return null;
});
cloudfunction์ ๋ด์ฅํจ์์์ firestore๋ฅผ ๋ชจ๋ํฐ๋งํ ์ ์๋ ํจ์๋ฅผ ๊ฐ๊ณ ์๋ค. ๋ณํ๊ฐ ์๊ธธ ๋ http๋ก ๋ง๋ค์ด์ง ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ ์๋๋ผ, ์ง์ firestore๋ฅผ ๋ฐ๋ผ๋ณด๊ฒ ํด์ ํด๋ผ์ด์ธํธ ์ชฝ์์ ํธ์ถํ์ง ์์๋ ํด๋ผ์ฐ๋ ํ๊ฒฝ์์ ๋ณํ๋ฅผ ๊ฐ์งํ๋ค. ์ด๋ ๊ฐ์ฅ ์ค์ํ๊ฒ collection ์ด๋ฆ์ ์คํ๋ฅผ ๋ด์ง ์๋ ๊ฒ์ด๋ค... ๋ด ๊ฒฝ์ฐ์๋ _ ํ๋๋ฅผ ์๋ชป ๋ฃ์ด์ 30๋ถ ๋์ ์๋ฌ ์์ธ์ ์ฐพ๊ณ ์์๋ค.
firestore์์ ๋ณํ๊ฐ ์๊ธด document๋ฅผ ์์์ ํ์งํ๋ค. ๊ทธ documentId๋ฅผ ํ ํฝ์ผ๋ก ์ฌ์ฉํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ณ์์ ์ ์ฅํด์ฃผ๊ณ , ๋ณํ๊ฐ ์๊ธด ๋ถ๋ถ์ change๋ฅผ ํตํด ์ ๊ทผํ ์ ์๋ค.
์์ฑํ ๋๋ ํธ์์๋ฆผ์ ๋ณด๋ผ ํ์๊ฐ ์์ด์ onUpdate๋ฅผ ์ฌ์ฉํ๋ค. onDelete๋ ํ์ฌ ์ํฉ์์ ์ฌ์ฉํ์ง๋ ์๋๋ฐ, ๋์ค์ ์ฌ์ฉํ ์๋ ์์ ๊ฒ ๊ฐ๋ค.
# FCM
const participants = newValue.participants;
const meetingContent = newValue.meeting;
const topic = documentId;
const message = {
notification: {
title: `${meetingContent}`,
body: `์ด์ ๋ชจ์์ ์ฐธ๊ฐ์๊ฐ ${participants.length}๋ช
์ด ๋์์ต๋๋ค`
},
topic: topic
};
return admin.messaging().send(message)
.then(response => {
console.log('Successfully sent message:', response);
return null;
})
.catch(error => {
console.error('Error sending message:', error);
return null;
});
fcm์ ๊ตฌ์ฑํด์ ์ ์กํ๋ ์ฝ๋๋ค. topic์ผ๋ก ์ฌ์ฉํ๋ ๊ฑด trigger์์ ๋ฐ์์จ document์ id์ด๊ณ , notification์ผ๋ก JSON ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ผํ๋ค.
์ด์ ๋ง๋ค์ด์ง ํจ์๋ฅผ ๊ฐ๊ณ emulators:start๋ก ํ ์คํธํ๋ค์ deployํด์ฃผ๋ฉด ๋๋๋ค...
๋ ์ฒ์์ cloudfunction์ผ๋ก ๋ ๋ฆฐ fcm์ data ๋ฉ์์ง๊ฐ ์๋๋ผ notification ์ผ์ค ์์๋ค. ๊ทธ๋ฐ๋ฐ ๋ฐ์๋ณด๋ data ๋ฉ์์ง๋ก ๋ค์ด์ค๊ณ ์์ด์ pending intent๋ฅผ ์ฌ์ฉํ ์ ์ด๋ ๊ฐ๋ฅํ๋ค.
๋์์ด ๋๋ค๋ฉด ๋๊ธ์ด๋ ๊ณต๊ฐ ๋ฒํผ ํ ๋ฒ์ฉ ๋๋ฅด๊ณ ๊ฐ์ฃผ์ธ์!
'Android ๐ฅ๏ธ > ์ฝ์งโ๏ธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Widget ์์๋ณด๊ธฐ(1) - ์์ ฏ์์ ์์น ์ ๋ณด ๋ฐ๊ธฐ (0) | 2024.07.09 |
---|---|
Kotlin ํ๊ธ ์ข ์ฑ ๊ตฌ๋ถํ๊ธฐ - ์ฒด์ธ์ ๋ฐ๋ผ ์กฐ์ฌ ๋ค๋ฅด๊ฒ ๋ถ์ด๊ธฐ (0) | 2024.05.24 |
๊ตฌ๊ธ ๋ก๊ทธ์ธ CredentialManager ๋ง์ด๊ทธ๋ ์ด์ + Firebase Auth (4) | 2024.05.11 |
Location Service ์ฝ์งํ๊ธฐ - ๋ด ์์น ๋ฐ์์ค๊ธฐ (0) | 2024.05.03 |
Firebase CloudFunction ์ฝ์ง + FCM(1) (0) | 2024.04.21 |