[英]Send a notification to another device
I want to be able to send a notification to another device on android. 我希望能够向Android上的另一台设备发送通知。 Currently, on my app, users can upload jobs they want doing and other users can place bids on that job. 目前,在我的应用程序上,用户可以上传他们想做的工作,其他用户可以对该工作出价。 The user can then accept a bid. 然后,用户可以接受出价。 When the user has accepted a bid a notification or message needs to be sent to the user who placed the bid. 用户接受出价后,需要将通知或消息发送给下标的用户。 The database that I'm using is Firebase. 我正在使用的数据库是Firebase。 Each user has an account which they sign in with and a unique ID. 每个用户都有一个用于登录的帐户和一个唯一的ID。
The only things that I have found is sending notifications to my own device. 我发现的唯一内容是将通知发送到我自己的设备。
It's easy to implement custom notifications with firebase. 使用Firebase实施自定义通知很容易。 When the bid is placed, write the user's token and the message in a node in firebase 出价后,将用户的令牌和消息写在firebase的节点中
notificationRef.push.setValue(new NotificationModel("bid accepted", firebaseUser().getToken()))
Now to send the notification, we will use firebase functions 现在发送通知,我们将使用firebase函数
Install firebase with node 使用节点安装Firebase
npm install -g firebase-tools
Import firebase 导入火力基地
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
then send the notification when the notification node
in firebase
is written 然后在编写firebase
的通知node
时发送通知
exports.bidNotification = functions.database.ref('/notification/{pushId}').onWrite((event) => {
const data = event.data;
console.log('Notification received');
if(!data.changed()){
console.log('Nothing changed');
return;
}
const payLoad = {
notification:{
title: 'App name',
body: data.val().message,
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60*60
};
return admin.messaging().sendToDevice(data.val().token, payLoad, options);
});
Finally, deploy your functions on firebase using the CLI Here is more: https://firebase.google.com/docs/functions/get-started 最后,使用CLI在Firebase上部署功能。更多信息: https : //firebase.google.com/docs/functions/get-started
Enjoy 请享用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.