Firebase 功能:通过 FCM 发送的推送通知的客户端网络套接字断开连接错误
Firebase Functions: Client network socket disconnected error for push notification sent via FCM
我正在使用 firebase 云函数向我的离子应用程序发送推送通知。在早期,一切正常,但最近开始出现新的错误,如下所示。它只是随机(在我看来)发生在函数执行中,导致没有发送推送通知。
我联系了 Firebase 支持,他们解释说它一定是我的防火墙的问题,因为它无法建立连接之类的,但是代码是通过 firebase 部署的,所以执行该功能的不是我的笔记本电脑,而是 google 云函数在云上执行,所以如果那是问题,那么它一定是在他们不理解的方面,或者我错了吗?
无论如何,这是我通过命令 'firebase deploy --only functions' 部署的错误和代码,在部署过程中没有显示任何错误。
代码:
exports.sendDebugPush = functions.pubsub.schedule('* * * * *').onRun((context) => {
console.log('Bun');
admin.database().ref('/users/R1CFRqnvsmdJtxIJZIvgF1Md0lr1').once("value", function(user) {
let todos = [];
for(let key in user.val().nextActions) {
if(user.val().nextActions[key].active != false) {
let todo = user.val().nextActions[key]
todo['todoid'] = key;
todos.push(todo);
}
}
if(todos.length > 0) {
//currently we pick a random todo, later on the one with the highest priority
//todos.sort((a, b) => (a.priority/1 < b.priority/1) ? 1 : -1);
let randomTodo = todos[Math.floor(Math.random()*todos.length)]
let payload = {
notification: {
title: "Gossik",
body: "Hoiiiii"
},
data: {
title: "Gossik",
body: "Hoiiiii",
target: 'todo',
todoid: randomTodo.todoid
}
};
admin.database().ref('/users/' + user.key + '/devices').once("value", function(devices) {
devices.forEach(function(device) {
admin.messaging().sendToDevice(device.val(), payload)
.catch(err => {
console.log('caaaatchiiiiiing');
console.log(err);
});
});
});
let pushNotification = {
message: "Hoiiiii",
todoid: randomTodo.todoid,
createDate: new Date().toISOString()
}
admin.database().ref('/users/' + user.key + '/pushNotifications').push(pushNotification);
}
});
return null;
});
错误发生在这一行:
admin.messaging().sendToDevice(device.val(), payload)
其他一切正常,甚至是我将通知推送到我的数据库之后的代码,所以我在我的数据库中获得了一个新条目,但 phone.
上没有推送通知
错误:
{ Error: Error while making request: Client network socket disconnected before secure TLS connection was established. Error code: ECONNRESET
at FirebaseAppError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseAppError (/workspace/node_modules/firebase-admin/lib/utils/error.js:123:28)
at /workspace/node_modules/firebase-admin/lib/utils/api-request.js:209:19
at process._tickCallback (internal/process/next_tick.js:68:7)
errorInfo:
{ code: 'app/network-error',
message:
'Error while making request: Client network socket disconnected before secure TLS connection was established. Error code: ECONNRESET' },
codePrefix: 'app' }
非常感谢任何帮助,谢谢!
您的代码始终没有正确处理承诺。每次你调用一个异步 API 那个 returns 一个承诺,这个承诺需要在你的函数终止之前完全解决。如果您不等待承诺解决,那么异步工作很可能永远不会完成,因为 Cloud Functions 会在代码完成之前终止它。请read the documenation about this.
由于您的函数只是立即 returns null,它不会等待任何工作完成。这包括对 FCM 的调用和数据库读取。 您将不得不重写此函数,使其returns成为一个仅在所有其他承诺都已解决后才解决的新承诺。
我正在使用 firebase 云函数向我的离子应用程序发送推送通知。在早期,一切正常,但最近开始出现新的错误,如下所示。它只是随机(在我看来)发生在函数执行中,导致没有发送推送通知。
我联系了 Firebase 支持,他们解释说它一定是我的防火墙的问题,因为它无法建立连接之类的,但是代码是通过 firebase 部署的,所以执行该功能的不是我的笔记本电脑,而是 google 云函数在云上执行,所以如果那是问题,那么它一定是在他们不理解的方面,或者我错了吗?
无论如何,这是我通过命令 'firebase deploy --only functions' 部署的错误和代码,在部署过程中没有显示任何错误。
代码:
exports.sendDebugPush = functions.pubsub.schedule('* * * * *').onRun((context) => {
console.log('Bun');
admin.database().ref('/users/R1CFRqnvsmdJtxIJZIvgF1Md0lr1').once("value", function(user) {
let todos = [];
for(let key in user.val().nextActions) {
if(user.val().nextActions[key].active != false) {
let todo = user.val().nextActions[key]
todo['todoid'] = key;
todos.push(todo);
}
}
if(todos.length > 0) {
//currently we pick a random todo, later on the one with the highest priority
//todos.sort((a, b) => (a.priority/1 < b.priority/1) ? 1 : -1);
let randomTodo = todos[Math.floor(Math.random()*todos.length)]
let payload = {
notification: {
title: "Gossik",
body: "Hoiiiii"
},
data: {
title: "Gossik",
body: "Hoiiiii",
target: 'todo',
todoid: randomTodo.todoid
}
};
admin.database().ref('/users/' + user.key + '/devices').once("value", function(devices) {
devices.forEach(function(device) {
admin.messaging().sendToDevice(device.val(), payload)
.catch(err => {
console.log('caaaatchiiiiiing');
console.log(err);
});
});
});
let pushNotification = {
message: "Hoiiiii",
todoid: randomTodo.todoid,
createDate: new Date().toISOString()
}
admin.database().ref('/users/' + user.key + '/pushNotifications').push(pushNotification);
}
});
return null;
});
错误发生在这一行:
admin.messaging().sendToDevice(device.val(), payload)
其他一切正常,甚至是我将通知推送到我的数据库之后的代码,所以我在我的数据库中获得了一个新条目,但 phone.
上没有推送通知错误:
{ Error: Error while making request: Client network socket disconnected before secure TLS connection was established. Error code: ECONNRESET
at FirebaseAppError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseAppError (/workspace/node_modules/firebase-admin/lib/utils/error.js:123:28)
at /workspace/node_modules/firebase-admin/lib/utils/api-request.js:209:19
at process._tickCallback (internal/process/next_tick.js:68:7)
errorInfo:
{ code: 'app/network-error',
message:
'Error while making request: Client network socket disconnected before secure TLS connection was established. Error code: ECONNRESET' },
codePrefix: 'app' }
非常感谢任何帮助,谢谢!
您的代码始终没有正确处理承诺。每次你调用一个异步 API 那个 returns 一个承诺,这个承诺需要在你的函数终止之前完全解决。如果您不等待承诺解决,那么异步工作很可能永远不会完成,因为 Cloud Functions 会在代码完成之前终止它。请read the documenation about this.
由于您的函数只是立即 returns null,它不会等待任何工作完成。这包括对 FCM 的调用和数据库读取。 您将不得不重写此函数,使其returns成为一个仅在所有其他承诺都已解决后才解决的新承诺。