Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BurningDroid/7a7edd64d3038c63a8563d1fe4d879c9 to your computer and use it in GitHub Desktop.
Save BurningDroid/7a7edd64d3038c63a8563d1fe4d879c9 to your computer and use it in GitHub Desktop.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
exports.myUpdateTrigger = functions.firestore
.document('recruits/{recruitId}')
.onUpdate(event => {
...
// 1. Firestore에서 message를 받을 사용자에 해당하는 document 검색
var userRef = db.collection('users').doc(recruiterId);
var getUserDoc = userRef.get()
.then(doc => {
if (doc.exists) {
// 2. 사용자 document에서 이전에 저장해둔 fcmToken을 추출
const token = doc.data().fcmToken;
// 3. 사용자에게 보낼 Message 작성
const payload = {
data: {
title: "New Applicant!",
body: "There is a new applicant.",
recruitId: recruiterId
}
};
// 4. 사용자에게 Message 전송
admin.messaging().sendToDevice(token, payload);
} else {
console.log('notifyNewApplicant - No such document!');
}
})
.catch(err => {
console.log('[ERR] Error getting document', err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment