I've implemented a scheduled function in Firebase (1st generation) that runs every 5 minutes. The code snippet below is a simplified version of the function, which shows just the problematic part:
initializeApp()
export const example = runWith({
timeoutSeconds: 20,
memory: '128MB',
maxInstances: 1,
minInstances: 0,
})
.region('europe-west1')
.pubsub.schedule('*/5 * * * *')
.onRun(async () => {
const date = new Date();
const weekday = date.getDay();
const hour = date.getHours();
const minute = Math.floor(date.getMinutes() / 15) * 15;
const topicName = `${weekday}-${hour}-${minute}`;
const messaging = getMessaging();
try {
await messaging.sendToTopic(
topicName,
{
data: {
mode: '15MinSchedule',
type: topicName,
},
},
{
priority: 'high',
},
);
debug(`Successfully sent to topic: ${topicName}`);
} catch (err) {
warn(`Error sending to topic ${topicName}:`, err);
}
});
The issue occurs intermittently, approximately every 2 to 3 hours, when the sendToTopic
method throws the following error (the full log is provided at the end of this post):
"error":"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1"
Based on the error message, it appears that the function is attempting to access a deprecated FCM-endpoint (v1). I'm not sure, but I think the firebase-admin package should already be using the correct, non-deprecated endpoint without requiring any manual migration, right? However, most of the time everything works fine and as I said, it just throws the error around once every 2 hours, after which it sends the messages correctly again.
What I have already tried
Package version:
"firebase-admin": "^12.3.1",
"firebase-functions": "^5.0.1",
Error log:
{
"textPayload": "FirebaseMessagingError: An unknown server error was returned. Raw server response: \"{\"error\":\"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1\"}\"\n at FirebaseMessagingError.fromServerError (/workspace/node_modules/firebase-admin/lib/utils/error.js:269:16)\n at createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:35:47)\n at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:80:75\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async sendToTopic (/workspace/lib/scheduled/***.js:73:9)\n at async Promise.all (index 1)\n at async exports.default (/workspace/lib/scheduled/***.js:43:5)\n at async /workspace/lib/group-functions/pubsub-group.js:56:5 {\n errorInfo: {\n code: 'messaging/unknown-error',\n message: 'An unknown server error was returned. Raw server response: \"{\"error\":\"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1\"}\"'\n },\n codePrefix: 'messaging'\n}",
"insertId": "66cdb534000ecdfcc85c9c9e",
"resource": {
"type": "cloud_function",
"labels": {
"function_name": "pubsub-min5Schedule",
"region": "europe-west1",
"project_id": "***"
}
},
"timestamp": "2024-08-27T11:15:00.970236Z",
"severity": "WARNING",
"labels": {
"runtime_version": "nodejs20_20240818_20_16_0_RC00",
"execution_id": "***",
"instance_id": "***"
},
"logName": "projects/***/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/***/traces/cee5cfbbaf474cae746709dafadd9785",
"receiveTimestamp": "2024-08-27T11:15:00.995052585Z"
}
I found the solution by myself:
As of August 22, 2024, the sendToTopic
and sendToCondition
methods have been deprecated, and we now need to use send
for both. I've run this for a day, and everything is working smoothly without any issues.