I'm working on the group functionality for my react-native app. And I wish to send cloud messages to users who have been added when a group is created. I'm using cloud functions to do that.
But I am getting this error in my function:
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
Its unable to fetch the fcm-token from firestore to send the notification.
I had written cloud functions for sending friend requests and in that, the token is retrieved successfully from cloud firestore, and the notification is sent.
This is my cloud function :
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
//======================NOTIFY ADDED MEMBERS==========================//
exports.notifyAddedMembers = functions.https.onCall((data, context) => {
const members = data.members;
const groupName = data.groupName;
var tokens = [];
members.forEach(async member => {
//send notifications to member.uid
console.log('MEMBER.UID ', member.uid);
await fetchTokenFromUid(member.uid)
.then(token => {
console.log('retrieved token: ', token);
// tokens.push(token);
const payload = {
notification: {
title: `You have been added to ${groupName}`,
body: 'Share your tasks',
sound: 'default',
},
};
return admin.messaging().sendToDevice(token, payload);
})
.catch(err => console.log('err getting token', err));
});
// console.log('ALL TOKENS: ', tokens);
console.log('GROUP NAME: ', groupName);
});
async function fetchTokenFromUid(uid) {
var token = '';
return await admin
.firestore()
.collection('Users')
.doc(`${uid}`)
.get()
.then(async doc => {
console.log('uid token: ', Object.keys(doc.data().fcmTokens));
var tokenArray = Object.keys(doc.data().fcmTokens); //ARRAY
for (var i = 0; i < tokenArray.length; i++) {
token = tokenArray[i]; //Coverts array to string
}
return token; //return token as string
});
}
I am using the react-native-firebase library.