我正在尝试在将某些内容添加到我的Firebase数据库后发出推送通知。
我这样做是参考这个博客的。如果某些内容添加到我的数据库中,我正在尝试提供推送通知
以下用于在 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);
}
这是我的索引.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:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRnode_modulesfirebase-adminlibutilserror.js:25:28)
at new FirebaseAppError (C:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRnode_modulesfirebase-adminlibutilserror.js:70:23)
at new Certificate (C:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRnode_modulesfirebase-adminlibauthcredential.js:108:19)
at new CertCredential (C:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRnode_modulesfirebase-adminlibauthcredential.js:174:33)
at Object.cert (C:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRnode_modulesfirebase-adminlibfirebase-namespace.js:175:58)
at Object.<anonymous> (C:UsersuserAppDataLocalTempfbfn_158889RLxz6KyCrVRindex.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");
在您的评论中,您说您正在使用云函数。在这种情况下,您的代码在Google的环境中运行,您甚至不需要google-services.json
。从此示例中:
firebase.initializeApp(functions.config().firebase);