gpt4 book ai didi

node.js - "Certificate object must contain a string "private_key "property"尝试发送推送通知时

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:30 28 4
gpt4 key购买 nike

我正在尝试在将某些内容添加到我的 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 文件似乎存在问题。

您是否按照博客文章的指示下载了该文件?如果有,你把它放在哪里?您确定您的文件路径与您放置文件的路径相匹配吗?我期望类似的东西:

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

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

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

关于node.js - "Certificate object must contain a string "private_key "property"尝试发送推送通知时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45350411/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com