40 lines
925 B
JavaScript
40 lines
925 B
JavaScript
const functions = require("firebase-functions");
|
|
const admin = require("firebase-admin");
|
|
|
|
admin.initializeApp();
|
|
|
|
exports.notifBurung = functions.database
|
|
.ref("/Kebun/Sensor/Deteksi") // ✅ TANPA SPASI
|
|
.onUpdate(async (change, context) => {
|
|
|
|
const before = change.before.val();
|
|
const after = change.after.val();
|
|
|
|
// kirim notif hanya saat berubah ke "burung"
|
|
if (after === "burung" && before !== "burung") {
|
|
|
|
const snapshot = await admin
|
|
.database()
|
|
.ref("/Kebun/Token")
|
|
.once("value");
|
|
|
|
const token = snapshot.val();
|
|
|
|
if (!token) {
|
|
console.log("Token tidak ditemukan");
|
|
return null;
|
|
}
|
|
|
|
const message = {
|
|
notification: {
|
|
title: "⚠️ Hama Burung!",
|
|
body: "Burung terdeteksi di kebun"
|
|
},
|
|
token: token,
|
|
};
|
|
|
|
return admin.messaging().send(message);
|
|
}
|
|
|
|
return null;
|
|
}); |