Reputation: 47
I am writing a cloud function so when a new name is added, notification is triggred.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.pushNotification = functions.database.ref('/name/{pushId}')
.onWrite(( change,context) => {
console.log('Push notification event triggered');
const payload = {
notification: {
title: 'App Name',
body: "New Vendor added",
sound: "default"
},
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24 //24 hours
};
return admin.messaging().sendToDevice("notifications", payload, options);
})
Do I need to make any changes in the code or do I need to make changes to android app? Function will deploy without any error. But when I add data to it, No logs in cloud function or no notification can be observed. Relatively new to JS, help will be highly appreciated.
Upvotes: 0
Views: 60
Reputation: 599571
Your Cloud Function trigger when there is a write to '/name/{pushId}'
. But your JSON doesn't show any top-level name
key, so it makes sense that the function never gets called.
It looks like you want to trigger the Cloud Function when data is written to /Vendor/{pushId
, so you should declare your trigger as:
exports.pushNotification = functions.database.ref('/Vendor/{pushId}')
.onWrite(( change,context) => {
...
This is really quite basic Cloud Functions stuff, but I can imagine it is hard to get right if you've never written JavaScript before. If you are new to JavaScript, Cloud Functions for Firebase is not the easiest way to learn it. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.
Upvotes: 1