I am getting the following Error: Operation is not implemented, or supported, or enabled. Raw server response: "{"error":{"code":501,"message":"Operation is not implemented, or supported, or enabled.","status":"UNIMPLEMENTED"}}"
and this is my modified code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
const firestore = admin.firestore();
const kFcmTokensCollection = "fcm_tokens";
const kPushNotificationsCollection = "ff_push_notifications";
const kUserPushNotificationsCollection = "ff_user_push_notifications";
const kSchedulerIntervalMinutes = 1;
const kPushNotificationRuntimeOpts = {
timeoutSeconds: 540,
memory: "2GB"
};
exports.sendPushNotifications = functions.region('asia-south1').https.onCall(
async (data, context) => {
console.log('Function started');
if (!context.auth.uid) {
console.log('Unauthenticated call');
throw new functions.https.HttpsError('unauthenticated', 'Unauthenticated calls are not allowed.');
}
const notificationData = data.notificationData;
const userDocRefs = data.userDocRefs;
const { title, body } = notificationData;
const sound = "custom_sound_notification.mp3";
if (!title || !body || !Array.isArray(userDocRefs) || userDocRefs.length === 0) {
console.log('Invalid arguments');
throw new functions.https.HttpsError('invalid-argument', 'Invalid arguments encountered when sending push notifications.');
}
const tokens = new Set();
const getUserTokens = async () => {
for (const userDocRef of userDocRefs) {
const userTokens = await firestore
.doc(userDocRef)
.collection(kFcmTokensCollection)
.get();
userTokens.docs.forEach((token) => {
if (typeof token.data().fcm_token !== "undefined") {
tokens.add(token.data().fcm_token);
console.log(`Retrieved FCM token: ${token.data().fcm_token}`);
}
});
}
};
await getUserTokens();
console.log(`Total tokens retrieved: ${tokens.size}`);
const tokensArr = Array.from(tokens);
const messageBatches = [];
for (let i = 0; i < tokensArr.length; i += 500) {
const tokensBatch = tokensArr.slice(i, Math.min(i + 500, tokensArr.length));
const messages = tokensBatch.map(token => ({
message: {
notification: {
title,
body,
},
android: {
notification: {
sound,
},
},
apns: {
payload: {
aps: {
sound,
},
},
},
token,
}
}));
messageBatches.push(messages);
}
let numSent = 0;
await Promise.all(
messageBatches.map(async (messages) => {
try {
const responses = await Promise.all(messages.map(message =>
axios.post(`https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send`, message, {
headers: {
'Authorization': Bearer ${await admin.credential.applicationDefault().getAccessToken().access_token}
,
'Content-Type': 'application/json'
}
})
));
numSent += responses.length;
console.log(`Batch sent: ${responses.length} messages`);
} catch (error) {
console.error('Error sending batch:', error);
}
})
);
console.log(`Total notifications sent: ${numSent}`);
return { success: Push notifications sent successfully to ${numSent} devices.
};
}
);