-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfcm.js
More file actions
43 lines (38 loc) · 1.19 KB
/
fcm.js
File metadata and controls
43 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const admin = require('firebase-admin');
//const serviceAccount = require('./path/firebase-admin.json');
const serviceAccount = require('./path/angelguard-push-firebase-adminsdk-mkcok-cce94130f5.json');
const connect = async () => { //firebase 초기화
try {
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
console.log('FCM SDK Initialized');
}
} catch (error) {
console.error('FCM connection error:', error.message);
}
};
const sendNotification = async (token) => {
try {
const title = 'Angel Guard';
const body = '아이의 상태를 확인해 주세요';
const response = await admin.messaging().send({
token: token,
notification: {
title: title,
body: body,
},
});
console.log('Successfully sent message:', response);
return response; // Return response for further handling in app.js
} catch (error) {
console.error('Error sending message:', error);
throw error; // Propagate error to be handled in app.js
}
};
// Export the connect function and sendNotification function
module.exports = {
connect,
sendNotification,
};