I have the following index.js
and I want to create a function in order to send a notification to all my users.
When I deploy my function I get the error TypeError: functions.region is not a function
. How can I change the region of my function and being able to deploy it
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const regionalFunctions = functions.region('europe-west1');
admin.initializeApp();
exports.sendNotificationToAllUsers = regionalFunctions.https.onCall(async (data, context) => {
// Optional: Check if the user calling the function is authenticated or has specific roles
// if (!context.auth) {
// // Throwing an HttpsError so that the client gets the error details.
// throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.');
// }
const title = data.title || 'Default Title'; // Get title from the data sent by the client, or use a default
const body = data.body || 'Default Body'; // Get body from the data sent by the client, or use a default
const message = {
notification: {
title: title,
body: body,
},
data: {
page: 'highlights',
// All values in the 'data' payload MUST be strings
},
android: {
priority: 'high',
notification: {
sound: 'default',
}
},
topic: 'all', // Sending to a topic that all users are subscribed to.
// Or, you can use 'android_users' if you want to target only Android users
// and they are subscribed to that specific topic.
};
try {
const response = await admin.messaging().send(message);
console.log('Successfully sent message:', response);
return {
success: true,
message: 'Notification sent successfully to all users.',
messageId: response
};
} catch (error) {
console.error('Error sending message:', error);
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('internal', 'Error sending notification.', error);
}
});
node -v v22.15.1
npm -v 10.9.0
The problem in your code lies here:
const functions = require('firebase-functions');
// 👆
In order to make it work, you need to specify the version:
const functions = require('firebase-functions/v1'); //See the /v1 import?
// 👆
Now you'll be able to deploy v.1 functions and use the corresponding region.