【问题标题】:How to send notifications from node.js server to android client.如何从 node.js 服务器发送通知到 android 客户端。
【发布时间】:2018-08-19 01:08:51
【问题描述】:

我需要使用什么技术从node.js服务器向android客户端发送通知。例如,用户A将用户B添加为好友,此时用户B应该会收到通知用户A 想将其添加到他的Android 设备上。我是 node.js 的新手,你能帮我具体用什么来实现发送此类通知。

【问题讨论】:

    标签: node.js express push-notification


    【解决方案1】:

    您可以使用 MQTT 或 AMQP 消息传递,这些是非常灵活的技术,非常适合向客户端推送消息。

    https://en.wikipedia.org/wiki/MQTT

    https://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol

    Node.js 对两者都有很好的支持。 Android 有一个可用的 MQTT 客户端,这里有一个示例:http://androidkt.com/android-mqtt/

    基本上,您可以通过以下方式向客户端推送消息:

    client.publish(主题、消息)。

    客户会像这样订阅:

    client.on('message', function (topic, message) {
      // Messages are Buffer objects.
      console.log(message.toString())
      client.end()
    })
    

    客户端会使用回调或轮询来接收此消息。

    两种技术都使用一个代理作为消息之间的中间人。

    您可以使用免费的在线经纪人来测试消息传递,例如mqtt://test.mosquitto.org

    在 Express 中,初始化消息传递客户端后,您可以就新事件、POST、PUTS 等发送消息。

    app.post("/addFriend", function(req, res, next){
        console.log("Friend request added");
    
        // Write to db.
    
        // Send a message 
        mqttClient.publish('friends-topic', JSON.stringify({event: 'newfriend', id: '10122', name: 'Mark' }))
    
        res.end('ok', 200);
    });
    

    【讨论】:

      【解决方案2】:

      在服务器端,您需要使用 Google 的云消息服务,例如 node-gcm 模块

      https://github.com/ToothlessGear/node-gcm

      【讨论】:

        猜你喜欢
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-06
        • 1970-01-01
        • 1970-01-01
        • 2018-06-16
        相关资源
        最近更新 更多