Skip to content

Instantly share code, notes, and snippets.

@ologunB
Created December 28, 2021 01:41
Show Gist options
  • Save ologunB/380fd1190995b8f45156336fe37f3c14 to your computer and use it in GitHub Desktop.
Save ologunB/380fd1190995b8f45156336fe37f3c14 to your computer and use it in GitHub Desktop.
exports.sendToAllUsers = functions.https.onCall(async (snap, context)=>{
const payload = {
notification:{
title : 'New Trivia' ,
body : 'A new Trivia is availble on the app, check now',
badge : '1',
sound : 'default'
},
data: {
type: 'new-trivia',
click_action: "FLUTTER_NOTIFICATION_CLICK",
status: "done",
}
}
return admin.firestore().collection('Tokens').get().then(tokensSnap=> {
if (tokensSnap != null) {
const results = [];
tokensSnap.forEach(doc => {
results.push(doc.data().token);
});
console.log('sent to '+ results.length + ' users');
return admin.messaging().sendToDevice(results,payload);
} else {
return console.log("=========there was an errror ==================");
}
}).catch((error) => {
return console.log(`Error ${error}`);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment