简体   繁体   English

发送通知到另一台设备

[英]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 Node.js 安装Node.js

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.

相关问题 如何使用电话号码将通知从Android应用发送到另一个Android设备? - How to send a notification from an android app to another android device using phone number? 将发布通知发送到特定的UUID android设备 - Send post notification to specific UUID android device Android使用GCM和PHP将通知从设备发送到其他设备 - Android send notification from device to other device using GCM and PHP 如何通过Java服务器使用FCM向Android设备发送推送通知? - how to send push notification to the android device using FCM by java server? 从Java服务器向服务器发送推送通知 - Send push notification from server to android device in Java 获取接收者的设备令牌以在 Firebase 中发送通知 - Get receiver's device token to send notification in Firebase Azure移动服务:将推送通知发送到特定的Android设备 - Azure mobile service : send push notification to specific android device 如何通过java服务器使用GCM向Android设备发送推送通知? - how to send push notification to the android device using GCM by java server? APNS-无法使用Java将通知发送到iOS设备 - APNS - Can't send notification to iOS device using Java 谷歌云消息发送通知,但我的设备从未被推送 - google cloud message send notification but my device is never pushed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM