Пытаюсь отправлять уведомления всем зарегистрированным пользователям от администратора Firebase каждые 2 часа с функцией расписания pubsub
Это код, который развертывается в функциях Firebase
exports.scheduledFunction =functions.pubsub.schedule('every 2 hours').onRun((context) => {
console.log('This will be run every 2 hours!');
listAllUsers(undefined)});
function listAllUsers(nextPageToken) {
// List batch of users, 1000 at a time.
admin.auth().listUsers(1000, nextPageToken)
.then(function(listUsersResult) {
listUsersResult.users.forEach(function(userRecord) {
pushCheckMessageNotification(userRecord.customClaims['latest']);
});
if (listUsersResult.pageToken) {
// List next batch of users.
listAllUsers(listUsersResult.pageToken);
}
})
.catch(function(error) {
console.log('Error listing users:', error);
});
}
function pushCheckMessageNotification(token){
const promise = [];
promise.push(admin.messaging().send(
{
data: {
body: "Test",
title: "Test",
chat: "CHECK_MESSAGES"
},
android:{
priority: "high"
},
token: '' + token,
}).then(() => {
console.log("Pushed notification.");
console.log("Notify Token", token);
}).catch(error => {
console.error("Notification push crash", error);
}));
}
Иногда возникала следующая ошибкаи уведомление не отправлено
{ Error: Error while making request: timeout of 10000ms exceeded.
at FirebaseAppError.Error (native)
at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:42:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:88:28)
at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:122:28)
at /user_code/node_modules/firebase-admin/lib/utils/api-request.js:152:23
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
errorInfo:
{ code: 'app/network-timeout',
message: 'Error while making request: timeout of 10000ms exceeded.' },
codePrefix: 'app' }