I followed a tutorial to set up the Firebase Admin SDK.
https://firebase.google.com/docs/admin/setup
I downloaded a JSON file (service account) from the Firebase console located at:
C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
I added an environment variable:
GOOGLE_APPLICATION_CREDENTIALS=C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
However, upon running the script, a Warning is displayed:
Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
How can I address this issue and test the code? I intend to send Push notifications in the onCreate event.
The code snippet is as follows:
"use strict";
exports.__esModule = true;
var functions = require("firebase-functions");
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://cargo-tender.firebaseio.com'
});
exports.sendPushNotification = functions.database
.ref('/user-chat')
.onCreate(function (event) {
var payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default'
}
};
return admin
.database()
.ref('fcmToken')
.once('value')
.then(function (allToken) {
if (allToken.val()) {
var token = Object.keys(allToken.val());
return admin
.messaging()
.sendToDevice(token, payload)
.then(function (response) {
//
});
}
});
});
Software versions:
PS C:\ct\functions> tsc -v
Version 3.5.2
PS C:\ct\functions> firebase -V
7.0.2
PS C:\ct\functions> node -v
v10.16.0
PS C:\ct\functions> npm -v
6.9.0