我使用本教程中,设置火力管理员SDK。
https://firebase.google.com/docs/admin/setup
我已经从downloaed控制台火力点一个JSON文件(服务帐户)。 这是该路径下:
C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
我已经设置环境变量:
GOOGLE_APPLICATION_CREDENTIALS=C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
当我运行该脚本,我收到以下警告:
Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
为什么? 而如何解决这个问题? 如何测试的代码? (我要发在OnCreate事件推送通知)
我的代码:
"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) {
//
});
}
});
});
版本:
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