我希望能够向安卓设备上的另一台设备发送通知。目前,在我的应用程序上,用户可以上传他们想要执行的工作,其他用户也可以对该工作出价。然后,用户可以接受出价。当用户接受出价后,需要向出价的用户发送通知或消息。我使用的数据库是Firebase。每个用户都有一个登录时使用的帐户和一个唯一的 ID。
我唯一发现的是向我自己的设备发送通知。
使用 Firebase 实现自定义通知很容易。出价后,将用户的令牌和消息写入 Firebase 的节点中
notificationRef.push.setValue(new NotificationModel("bid accepted", firebaseUser().getToken()))
现在要发送通知,我们将使用 Firebase 函数
安装节点.js
使用节点安装火力底座
npm install -g firebase-tools
导入火力基地
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
然后在写入通知node
时发送通知 firebase
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);
});
最后,使用 CLI 在 Firebase 上部署函数这里还有更多: https://firebase.google.com/docs/functions/get-started
享受