繁体   English   中英

发送通知到另一台设备

[英]Send a notification to another device

我希望能够向Android上的另一台设备发送通知。 目前,在我的应用程序上,用户可以上传他们想做的工作,其他用户可以对该工作出价。 然后,用户可以接受出价。 用户接受出价后,需要将通知或消息发送给下标的用户。 我正在使用的数据库是Firebase。 每个用户都有一个用于登录的帐户和一个唯一的ID。

我发现的唯一内容是将通知发送到我自己的设备。

使用Firebase实施自定义通知很容易。 出价后,将用户的令牌和消息写在firebase的节点中

notificationRef.push.setValue(new NotificationModel("bid accepted", firebaseUser().getToken()))

现在发送通知,我们将使用firebase函数


安装Node.js

使用节点安装Firebase

npm install -g firebase-tools

导入火力基地

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

然后在编写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);


});

最后,使用CLI在Firebase上部署功能。更多信息: https : //firebase.google.com/docs/functions/get-started

请享用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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