Ich versuche, eine Push-Benachrichtigung über Firebase/Node JS zu senden. Ich habe diesen Code derzeit auf Heroku laufen.Fehler bei der Firebase-Authentifizierung auf Knoten JS: NotAuthorizedError
var Firebase = require('firebase');
var request = require('request');
var express = require('express');
var FCM = require('fcm-node');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/push', function(request, response) {
response.send("running");
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
// Planet Firebase API Key
var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key
var fcm = new FCM(API_KEY);
var config = {
apiKey: "AIz...",
authDomain: "XXX.firebaseapp.com", // Authentication > Sign-In Method > OAuth Redirect Domains
databaseURL: "https://XXX.firebaseio.com/", // Database > Url at the top
storageBucket: "gs://XXX.appspot.com", // Storage > Url at the top
};
var firebase = Firebase.initializeApp(config);
var ref = firebase.database().ref();
function listenForNotificationRequests() {
var requests = ref.child('notifications');
requests.on('child_changed', function(requestSnapshot) {
var objectAdded = requestSnapshot.val();
var uid = receiver["uid"]
var notificationMessage = objectAdded["message"]
sendNotificationToUser(uid, notificationMessage);
}, function(error) {
console.error(error);
});
};
function sendNotificationToUser(receiverID, notificationMessage) {
var message = {
to: '/notifications/'+receiverID,
notification: {
title: "App Title",
body: notificationMessage,
badge: 1
}
};
fcm.send(message, function(err, response){
if (response) {
console.log("Successfully sent with response: ", response);
} else {
console.log("Something has gone wrong! Error: " + err);
}
});
}
// start listening
listenForNotificationRequests();
Wenn fcm.send()
genannt wird, komme ich zurück:
Something has gone wrong! Error: NotAuthorizedError
Dies führt seine Firebase zu glauben, ist nicht richtig initialisiert wird, aber ich habe die Links und Tasten mehrmals geprüft. Was habe ich falsch gemacht?
Haben Sie eine detailliertere Fehlerreaktion? –
Leider nicht. Das ist alles, was es mir gibt. – user1072264
Haben Sie es ausprobiert? Meine Antwort meine ich. Konnten Sie nach der Verwendung des Serverschlüssels eine Nachricht senden? –