I'm trying to send out a push notification from a teacher to a student, for example. I'm using firebase and I've already got that token from each device and saved it on firebase to the wanted user, and setup my FirebaseMessagingService. Now I want to be able to send a notification, programmatically, whenever a teacher clicks on a student in a list, for example. How do I do that? I've been looking for a solution for a couple of days and couldn't find an answer. Thanks :)
-
If you consider at some point to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), I have exaplained in one of my tutorials step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. You can also take a look at my answer from this **[post](https://stackoverflow.com/questions/48298993/push-notifications-on-content-change/48299840)**. – Alex Mamo Feb 09 '19 at 10:24
2 Answers
This is what I did. And it works. I know, because I was up all night trying to get this thing to work.
admin.messaging().sendToDevice(token, msg)
.then(resMsg => {
console.log("Successfully sent message", resMsg);
}).catch(err => {
console.log("Error sending message", err);
})
Where token is the FCM token from the client device. And the msg is a JSON payload of the following structure:
const msg ={
notification: {
title: 'It\'s the last day of the week!',
body: `It's Sunday! Don't forget to refresh Dashboard with your latest activity data!`,
},
};
Source Build a transactional push notifications system using only Firebase

- 3,891
- 2
- 18
- 22
-
How are you defening 'token' values in this case? For examples, if I need send token only for feminine users ? it's possible ? – Diego Venâncio May 06 '21 at 12:03
For sending push notifications with firebase you need to use the FCM API which can be found here: https://firebase.google.com/docs/cloud-messaging/server.
Assuming you use Java for your software you could take a look at this quickstart example provided by google: https://github.com/firebase/quickstart-java/blob/master/messaging/src/main/java/com/google/firebase/quickstart/Messaging.java or look for a complete library.
You just need to add some logic to your application that triggers the FCM API with the correct token and payload.

- 167
- 2
- 14
-
I have all that, my project is not the issue here, I need the idea of where to begin looking for the solution, not the actual solution. I don't have a problem in my project, my project is working just fine, just a guidence of where to begin or which logic to follow – Adi Harel Feb 08 '19 at 20:50
-
Alright. Then I‘d recommend looking at the Google QuickStart Sample. That in connection with the API docs is the best starting point i.m.o – Robert Keck Feb 08 '19 at 20:53