Firebase cloud function doesn't work on Cordova - firebase

I'm trying to send a notification to every user in my Cordova app, at a schedule date automatically. I tried to create a cloud function, but the log on firebase don't show any error.
I tried to make this function, and I'm using the cordova-plugin-firebase-lib to receive notifications, if I go to the firebase console and send it manually it works
//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite( event => {
console.log('Push notification event triggered');
// Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
if(valueObject.photoUrl !== null) {
valueObject.photoUrl= "Sent you a photo!";
}
// Create a notification
const payload = {
notification: {
title:valueObject.name,
body: valueObject.text || valueObject.photoUrl,
sound: "default"
},
};
//Create an options object that contains the time to live for the notification and the priority
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic("pushNotifications", payload, options);
});
when i try to add name and text to message on my data base nothing happens

Related

Can't send Firebase Cloud Messages if function is called by Google Tasks

I have an .onCreate cloud function, that once triggered, creates a task and runs it.
If I simply run admin.messaging().sendToDevice(payload.tokens, payload.message); inside the exports.createScheduledNotification function, then it runs fine, but I wanted to utilise Cloud Tasks so that I can schedule the task a little further into the future.
However, even if the entire payload is inside the scheduledNotification function, it still doesn't run. I have a feeling that it's actually not being called at all. However, I've checked in the Google Cloud Tasks console, and it shows that it has run, but I get no response from the Firebase Messaging at all, so from that I can deduce it's not running.
Is there something wrong with how I'm sending the task request? Is the cloud function that receives the call written incorrectly?
Cloud Functions & Tasks Set up and Imports
'use strict';
const functions = require('firebase-functions');
var admin = require("firebase-admin");
const { CloudTasksClient } = require('#google-cloud/tasks')
var serviceAccount = require("./secret_file.json");
admin.initializeApp({
credential: admin.credential.cert(placeholder_for_account_credentials)
});
Function that is meant to create and send a Google Task, which is meant to call an .onRequest function, to send a Firebase Message
exports.createScheduledNotification = functions.firestore.document('/followers/{followedUid}')
.onCreate(async (snapshot) => {
const data = snapshot.data();
const tasksClient = new CloudTasksClient();
const queuePath = tasksClient.queuePath(project, location, queue);
const url = `https://{placeholder_for_location}-{placeholder_for_project_id}.cloudfunctions.net/scheduledNotification`;
const docPath = snapshot.ref.path;
const task = {
httpRequest: {
httpMethod: 'POST',
url,
body: Buffer.from(JSON.stringify({})).toString('base64'),
headers: {
'Content-Type': 'application/json',
},
},
scheduleTime: {
seconds: 10 + Date.now() / 1000
}
};
try {
await tasksClient.createTask({ parent: queuePath, task });
} catch (error) {
console.log(error);
}
});
Function to accept a call from Google Tasks, and then sends a message to a selected device_id
exports.scheduledNotification = functions.https.onRequest((req, res) => {
const payload = {
message: {
notification: {
title: 'You have a new follower!',
body: `Moe is now following you.`,
sound: 'default'
},
},
tokens: ["placeholder_for_device_id"]
};
admin.messaging().sendToDevice(payload.tokens, payload.message);
});
I would also like to mention that I saw in another question I needed to allow the principal firebase account to be able to create tasks, and I've added Cloud Task Admin and Cloud Task Enqeuer to every single account, and that the task are showing up in the Cloud Tasks Console.

How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

I'm working on a Firebase Cloud Function, to send triggered push notifications.
Right now my function sends a push as soon as an user triggers the "IAP" event in my app.
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendIAPAnalytics = functions.analytics.event('IAP').onLog((event) => {
const user = event.user;
const uid = user.userId; // The user ID set via the setUserId API.
sendPushToUser();
return true;
});
function sendPushToUser(uid) {
// Fetching all the user's device tokens.
var ref = admin.database().ref(`/users/${uid}/tokens`);
return ref.once("value", function(snapshot){
const payload = {
notification: {
title: 'Hello',
body: 'Open the push'
}
};
console.log("sendPushToUser ready");
admin.messaging().sendToDevice(snapshot.val(), payload)
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
This functions works, push are sent and received.
I read some news about scheduling for Firebase Cloud Functions:
https://medium.com/#pascalluther/scheduling-firebase-cloud-functions-with-cloud-scheduler-b5ec22ace683
https://firebase.googleblog.com/2019/04/schedule-cloud-functions-firebase-cron.html
I understood, it's only for HTTP triggers ou PUB/SUB triggers.
So for now it's always impossible to trigger functions with delays, by writing in realtime database or when analytics events are triggered.
Am I right? or is there a trick?
I read nothing about this.
EDIT: Official documentation
https://firebase.google.com/docs/functions/schedule-functions
My syntax is wrong but I need something like this:
function sendPushToUser(uid) {
var ref = admin.database().ref(`/users/${uid}/tokens`);
return ref.once("value", function(snapshot){
const payload = {
notification: {
title: 'Hello',
body: 'Open the push'
}
};
functions.pubsub.schedule('at now + 10 mins').onRun((context) => {
admin.messaging().sendToDevice(snapshot.val(), payload)
})
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
There is no built-in way to retrigger Cloud Functions with a delay. If you want such functionality you will have to build that yourself, for example by scheduling a function to run periodically and then see what tasks need to be triggered. See my answer here: Delay Google Cloud Function
As Doug commented, you can use Cloud Tasks to schedule individual invocations. You'd dynamically create the task, and then have it call a HTTP function.

React native notifications in a Chat App

I'm working on a chat application and I currently have 6 different chat channels with a different firebase database for each of them. Every channels have firebase rules that define if an user can read and write in this channel. I want to send notifications to users when a new message is posted, but only the users that are part of the specified channel.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const _ = require('lodash');
admin.initializeApp(functions.config().firebase);
exports.sendNewMessageNotification = functions.database.ref('/GeneralMessage').onWrite(event => {
const getValuePromise = admin.database()
.ref('GeneralMessage')
.orderByKey()
.limitToLast(1)
.once('value');
return getValuePromise.then(snapshot => {
console.log(_.values(snapshot.val())[0]);
const { text } = _.values(snapshot.val())[0];
const payload = {
notification: {
title: 'New msg',
body: text,
}
};
return admin.messaging()
.sendToTopic('GeneralMessage', payload);
});
});
That's the code I currently have using firebase cloud function in /functions/index.js. When I send a message in the app, I have this output in firebase : Firebase cloud functions but the notifications isn't working in app.

Flutter: Firebase Pushnotification On Data Change

After getting the comment, i have deployed this folowing code to my firebase project and it was successfully deploed!.But there is no notifications been send to me.
Please check my Firebase Realtime database Screenshot here for better understanding.
[ITS SOLVED NOW:IT WILL SEND NOTIFICATIONS TO ONLY ONE ID ie My Admin Device]
WORKING CODE:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firbase);
exports.codeformypeople = functions.database.ref('items/{id}').onWrite(evt => {
const payload = {
notification: { title: 'New Customer Requested', body: 'Touch to Open The App', badge: '1', sound: 'default', }
};
const token ="Lsn-bHfBWC6igTfWQ1-h7GoFMxaDWayKIpWCrzC";//replace with ur token
if (token) {
console.log('Toke is availabel .');
return admin.messaging().sendToDevice(token, payload);
} else {
console.log('token error');
}
});
[
SEE THIS VIDEO LINK FOR MORE DETAILS
note:If your app is opened and minmized then it will show notification,but if the app is opened and you are using,or if the app is terminated force close then it will not work!!
You can use firebase cloud function to trigger notification. Here is snippet of cloud functions which i am using to trigger notification:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref('/Notifications/{pushId}')
.onWrite(( change,context) => {
console.log("Push Notification event triggered");
var request = change.after.val();
var payload = {
data:{
username: request.userName,
}
};
admin.messaging().sendToDevice(request.userTokenId, payload)
.then(function(response){
console.log("Successfully sent message: ",response);
console.log(response.results[0].error);
})
.catch(function(error){
console.log("Error sending message: ", error)
})
})
Below i have provided my notification structure, you can see below.This function will trigger if any new data is being pushed in database notification node. To see what is output/error you are getting when this function is trigger go to firebase admin panel > Functions > Logs.
You have deployed function code perfectly, but you forgot to add refresh tokenId in your database as you can see in above picture i am saving userTokenId in my database and in function admin.messaging().sendToDevice(request.userTokenId, payload) i am using that tokenId, this tokenId is used to send notification to particular device, you can get this tokenId using FirebaseInstanceId.getInstance().getToken() and save this in your fbproject1 > items database sturcture. please refer this & this

dialogflow to interact with firebase realtime database

Is it possible to get some data from firebase database by using dialogflow? I'm new to dialogflow so I'm still doing some research about.
For example, I want to ask my chatbot if a doctor is available then chatbot will access the firebase db to check if that specific doctor is available or lets say schedule me an appoint with doc X so dialogflow will do a function that allow will enter a schedule object to the database
thanks.
You can use Firebase function to fulfill your Dialogflow agent and the Firestore database to store data. An example of how to do so with Dialogflow's Google Assistant integration is below:
const functions = require('firebase-functions');
const firebaseAdmin = require('firebase-admin');
const DialogflowApp = require('actions-on-google').DialogflowApp;
// Initialize Firebase Admin SDK.
firebaseAdmin.initializeApp(functions.config().firebase);
exports.dialogflowFulfillment = functions.https.onRequest((req, res) => {
// Log headers and body
console.log('Request headers: ' + JSON.stringify(req.headers));
console.log('Request body: ' + JSON.stringify(req.body));
// Create a new Dialgoflow app request handler
let app = new DialogflowApp({request: req, response: res});
// welcome function handler
function start(app) {
// Get user ID from the Google Assistant through Action on Google
let userId = app.getUser().userId;
// Check if the user is in our DB
admin.firestore().collection('users').where('userId', '==', userId).limit(1).get()
.then(snapshot => {
let user = snapshot.docs[0]
if (!user) {
// If user is not in DB, its their first time, Welcome them!
app.ask('Welcome to my app for the first time!');
// Add the user to DB
firebaseAdmin.firestore().collection('users').add({
userId: userId
}).then(ref => {
console.log('Added document with ID: ', ref.id);
});
} else {
// User in DB
app.ask('Welcome back!')
}
});
}
// Map function hanlder to Dialogflow's welcome intent action 'input.welcome'
const actionMap = new Map('input.welcome', start)
app.handleRequest(actionMap);
});

Resources