如何向特定用户的Firebase发送推送通知 [英] How to send push notifications to specific users firebase

查看:59
本文介绍了如何向特定用户的Firebase发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我正在尝试从老师向学生发送推送通知.我正在使用Firebase,并且已经从每个设备上获取了该令牌,并将其保存在Firebase上并保存到所需的用户,然后设置了FirebaseMessagingService.现在,我希望能够以编程方式发送通知,例如,当老师单击列表中的学生时.我怎么做?我一直在寻找解决方案几天,却找不到答案.谢谢:)

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);
})

其中令牌是来自客户端设备的FCM令牌.而味精是具有以下结构的JSON负载:

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!`,
             }, 
           };

来源构建一个仅使用Firebase的事务性推送通知系统

这篇关于如何向特定用户的Firebase发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆