39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import admin from "firebase-admin";
|
|
|
|
// Inisialisasi admin (hanya sekali)
|
|
if (!admin.apps.length) {
|
|
admin.initializeApp({
|
|
credential: admin.credential.cert({
|
|
projectId: process.env.GOOGLE_PROJECT_ID,
|
|
privateKey: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
|
|
clientEmail: process.env.GOOGLE_CLIENT_EMAIL,
|
|
}),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Kirim push notifikasi ke FCM token
|
|
*/
|
|
export async function sendFCM(token: string, title: string, body: string) {
|
|
const message = {
|
|
token,
|
|
notification: {
|
|
title,
|
|
body,
|
|
},
|
|
data: {
|
|
tested: "true"
|
|
}
|
|
};
|
|
|
|
try {
|
|
const result = await admin.messaging().send(message);
|
|
console.log("✅ FCM berhasil dikirim:", result);
|
|
} catch (err) {
|
|
console.error("❌ Gagal mengirim FCM:", err);
|
|
}
|
|
}
|
|
|
|
// const token = "cRz96GHKTRaQaRJ35e8Hxa:APA91bEUSxE0VPbqKSzseQ_zGhbYsDofMexKykRw7o_3z2aPM9YFmZbeA2enrmb3qjdZ2g4-QQtiNHAyaZqAT1ITOrwo9jVJlShTeABmEFYP5GLEUZ3dlLc";
|
|
// sendFCM(token, "Test dari Local", "Ini hanya percobaan notifikasi dari script.");
|