【问题标题】:Send a notification to another device向另一台设备发送通知
【发布时间】:2018-03-20 18:57:50
【问题描述】:

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

我发现的唯一东西就是向我自己的设备发送通知。

【问题讨论】:

标签: java android firebase firebase-realtime-database notifications


【解决方案1】:

使用 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

享受

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    相关资源
    最近更新 更多