簡體   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