HemalHerath HemalHerath - 2 years ago 71
Node.js Question

I need to send push notification after something added to firebase database how can i do that

https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html
I have done this referring to above blog.

//below code for creating database in firebase

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 = ".AAAA6J9B3gg:APA91bEw_oHJSrAFuRxB6k6TzJ6wU-yzEw64yaf7CwBq7ur2iroGqi7Cf5JZ127wW_sBFtfdtBkpz15pPTVdQtkGPoMf6uBTMgq5AdTwXVWUDhtxQiGx5Pkj2CIpPDi0xTNT_ZMiEmIG";

var serviceAccount = require("private/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 an error like below


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_12244fb0DXYqucY3u\node_modules\firebase-admin\lib\utils\error.js:25:28)
at new FirebaseAppError (C:\Users\user\AppData\Local\Temp\fbfn_12244fb0DXYqucY3u\node_modules\firebase-admin\lib\utils\error.js:70:23)
at new Certificate (C:\Users\user\AppData\Local\Temp\fbfn_12244fb0DXYqucY3u\node_modules\firebase-admin\lib\auth\credential.js:108:19)
at new CertCredential (C:\Users\user\AppData\Local\Temp\fbfn_12244fb0DXYqucY3u\node_modules\firebase-admin\lib\auth\credential.js:174:33)
at Object.cert (C:\Users\user\AppData\Local\Temp\fbfn_12244fb0DXYqucY3u\node_modules\firebase-admin\lib\firebase-namespace.js:175:58)
at Object.<anonymous> (C:\Users\user\AppData\Local\Temp\fbfn_12244fb0DXYqucY3u\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)


please help me with this it's really important

Answer Source

Did you install the request module ?

npm install request

Npm install documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download