Google App Engine Flexible 환경에서 firebase를 실행 중입니다. 노드에 리스너를 추가 한 자식을 추가하고 자식을 한 명 추가 할 때마다 두 번 호출됩니다. 이는 Google App Engine에서이 코드를 실행하는 경우에만 발생합니다. 로컬로 실행하면 예상대로 작동합니다. 코드는 다음과 같습니다Google App Engine에서 실행되는 동안 Firebase 기능이 두 번 호출됩니다.
// [START app]
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var firebase = require('firebase-admin');
var app = express();
firebase.initializeApp({
credential: firebase.credential.applicationDefault(),
databaseURL: "https://myurl.firebaseio.com/"
});
var db = firebase.database();
var globalNotifications = db.ref("/globalNotifications");
var API_KEY = "myKey"; // Your Firebase Cloud Server API key
var now = new Date();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
globalNotifications.orderByChild('processed').equalTo(0).on("child_added", function(snapshot) {
var key = snapshot.key;
var incomingNotification = snapshot.val();
var userID = incomingNotification.userID;
var notifyID = incomingNotification.notifyID;
request({
url: 'https://fcm.googleapis.com/fcm/send',
method: 'POST',
headers: {
'Content-Type' :'application/json',
'Authorization': 'key='+API_KEY
},
body: JSON.stringify({
"to" : incomingNotification.notificationID,
"priority" : "high",
"notification" : {
"body" : "someone is following you",
"title" : "You have a new follower"
}
})
}, function(error, response, body) {
if (error) { console.error(error); }
else if (response.statusCode >= 400) {
console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage);
}
else {
console.log(response.statusCode);
console.log(body);
globalNotifications.child(key).update({"processed": 1, "response": body});
}
});
});
// Start the server
var server = app.listen(process.env.PORT || '8080', function() {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
// [END app]
사전에 도움을 주셔서 감사합니다.
Google 애플리케이션 엔진 API보다 더 신뢰할 수있는 기능을 제공 해주셔서 감사합니다. –