"Certificate object must contain a string "private_key" 属性" 尝试发送推送通知时

"Certificate object must contain a string "private_key" property" when trying to send push notification

我正在尝试在将某些内容添加到我的 firebase 数据库后发出推送通知。

我参考 this blog 完成了此操作。如果我的数据库中添加了某些内容,我会尝试发出推送通知

在 firebase 中创建数据库的代码如下:

// I'm passing my message content and user here it will create a child in database

public static void sendNotificationToUser(String user, final String message) {
        DatabaseReference ref;
        ref = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference notifications = ref.child("notificationRequests");

        Map notification = new HashMap<>();
        notification.put("username", user);
        notification.put("message", message);

        notifications.push().setValue(notification);
    }

这是我的 index.js 脚本代码:

var firebase = require('firebase-admin');
var request = require('request');

var API_KEY = ".AAA...iEmIG";

var serviceAccount = require(".google-services.json");

firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "https://chitchatapp-73060.firebaseio.com/"
});
ref = firebase.database().ref();

function listenForNotificationRequests() {
    var requests = ref.child('notificationRequests');
    requests.on('child_added', function (requestSnapshot) {
        var request = requestSnapshot.val();
        sendNotificationToUser(
            request.username,
            request.message,
            function () {
                requestSnapshot.ref.remove();
            }
        );
    }, function (error) {
        console.error(error);
    });
};

function sendNotificationToUser(username, message, onSuccess) {
    request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
            'Content-Type': ' application/json',
            'Authorization': 'key=' + API_KEY
        },
        body: JSON.stringify({
            notification: {
                title: message
            },
            to: '/topics/user_' + username
        })
    }, function (error, response, body) {
        if (error) { console.error(error); }
        else if (response.statusCode >= 400) {
            console.error('HTTP Error: ' + response.statusCode + ' - ' + response.statusMessage);
        }
        else {
            onSuccess();
        }
    });
}
listenForNotificationRequests();

当我尝试部署它时,出现如下错误,我该如何解决

Error: Error occurred while parsing your function triggers.

Error: Certificate object must contain a string "private_key" property.
    at FirebaseAppError.FirebaseError [as constructor] (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\utils\error.js:25:28)
    at new FirebaseAppError (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\utils\error.js:70:23)
    at new Certificate (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\auth\credential.js:108:19)
    at new CertCredential (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\auth\credential.js:174:33)
    at Object.cert (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\node_modules\firebase-admin\lib\firebase-namespace.js:175:58)
    at Object.<anonymous> (C:\Users\user\AppData\Local\Temp\fbfn_158889RLxz6KyCrVR\index.js:12:37)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)

是什么导致了这个错误,我该如何解决?

您的 google-services.json 文件似乎有问题。

您是否按照博客 post 的指示下载了该文件?如果是这样,你把它放在哪里了?并且您确定匹配中放置文件的路径吗?我希望是这样的:

var serviceAccount = require("./google-services.json");

在您的评论中,您说您正在使用 Cloud Functions。在那种情况下,您的代码在 Google 的环境中运行,您甚至不需要 google-services.json。来自这个样本:

firebase.initializeApp(functions.config().firebase);