简体   繁体   中英

How to send push notifications to specific users firebase

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 :)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM