upd: push notifikasi
This commit is contained in:
13
xfcm/admin.ts
Normal file
13
xfcm/admin.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import admin from "firebase-admin";
|
||||
|
||||
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,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export default admin;
|
||||
38
xfcm/send.ts
Normal file
38
xfcm/send.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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.");
|
||||
Reference in New Issue
Block a user