Reputation: 1056
I'm trying give push notification after something added to my firebase database.
I have done this referring to this blog. I'm trying to give push notification if something added to my database
Below code for creating database in 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);
}
This is my index.js script code:
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();
When I try to deploy this it gives me an error like below how can I fixed that
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)
What is causing this error and how can I fix it?
Upvotes: 1
Views: 7698
Reputation: 599621
It looks like you have a problem in your google-services.json
file.
Did you download that file as the blog post instructed? If so, where did you put it? And are you sure the path in your matches where you put the file? I'd expect something like:
var serviceAccount = require("./google-services.json");
In your comment you say that you're using Cloud Functions. In that case your code runs in Google's environment and you don't even need the google-services.json
. From this sample:
firebase.initializeApp(functions.config().firebase);
Upvotes: 3