I created a function to send notifications via firebase, and I get this error:
Error fetching user data (a0ZdYX3IgCbssNNRP2oPZyAAEOt1): { Error: www.googleapis.com network timeout. Please try again.
at FirebaseAppError.Error (native)
at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:70:23)
at TLSSocket.<anonymous> (/user_code/node_modules/firebase-admin/lib/utils/api-request.js:106:51)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket.Socket._onTimeout (net.js:339:8)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
errorInfo:
{ code: 'app/network-timeout',
message: 'www.googleapis.com network timeout. Please try again.' } }
Is it because of me (firebase restriction for free?) Or a bug on google server and I just need to wait?
My function (I know that a function can be improved in speed, but the question does not exist, I just want to know if I am the cause of this error). This function sends a notification to users who installed the application x a day ago, retrieving the day of creating your account and their fcm token is stored in the database:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotificationToNewUser = functions.https.onRequest((request, response) => {
Date.prototype.sameDay = function(d) {
return this.getFullYear() === d.getFullYear()
&& this.getDate() === d.getDate()
&& this.getMonth() === d.getMonth();
}
var query = admin.database().ref("users").orderByKey();
var defaultAuth = admin.auth();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var user_id = childSnapshot.key;
var user_data = childSnapshot.val();
admin.auth().getUser(user_id)
.then(function(userRecord) {
var create_date = userRecord.metadata.createdAt
var date_now = new Date(Date.now());
console.log("Creation date:", create_date);
create_date.setDate(create_date.getDate()+4);
if (create_date.sameDay(date_now)) {
var registrationToken = user_data.fcm_token;
const payload = {
notification: {
body: "blabla",
sound: "default"
},
"data" : {
"key" : "XXX",
"value" : "playstore"
}
};
var options = {
priority: "high",
collapseKey: "playstore"
};
admin.messaging().sendToDevice(registrationToken, payload, options)
.then(function(response) {
console.log("Successfully sent message:", response);
})
.catch(function(error) {
console.log("Error sending message:", error);
});
}
})
.catch(function(error) {
console.log("Error fetching user data ("+user_id+"):", error);
});
});
});
response.send("OK");
})
filol source
share