Added device console push notification support.

This commit is contained in:
Ylian Saint-Hilaire 2021-01-31 04:31:32 -08:00
parent 2248cb0a6d
commit bd17264b05
5 changed files with 30 additions and 17 deletions

View File

@ -17,16 +17,26 @@
// Construct the Firebase object
module.exports.CreateFirebase = function (parent, senderid, serverkey) {
var obj = {};
obj.messageId = 1;
obj.messageId = 0;
const Sender = require('node-xcs').Sender;
const Message = require('node-xcs').Message;
const Notification = require('node-xcs').Notification;
const xcs = new Sender(senderid, serverkey);
var tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid }
// Messages received from client (excluding receipts)
xcs.on('message', function (messageId, from, data, category) {
console.log('Firebase-Message', messageId, from, data, category);
//console.log('Firebase-Message', messageId, from, data, category);
// Lookup node information from the cache
var ninfo = tokenToNodeMap[from];
if (ninfo == null) return;
if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid);
}
});
// Only fired for messages where options.delivery_receipt_requested = true
@ -41,11 +51,17 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
xcs.start();
// EXAMPLE
//var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } };
//var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High'
// Send an outbound push notification
obj.sendToDevice = function (token, payload, options, func) {
obj.sendToDevice = function (node, payload, options, func) {
if ((node == null) || (typeof node.pmt != 'string')) return;
// Fill in our lookup table
tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain }
// Built the on-screen notification
var notification = null;
if (payload.notification) {
@ -65,7 +81,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
// Send the message
function callback(result) { callback.func(result.getMessageId(), result.getError(), result.getErrorDescription()) }
callback.func = func;
xcs.sendNoRetry(message, token, callback);
xcs.sendNoRetry(message, node.pmt, callback);
}
return obj;

View File

@ -1546,7 +1546,7 @@ function CreateMeshCentralServer(config, args) {
// Setup Firebase
if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) {
obj.firebase = require('./firebase').CreateFirebase(this, config.firebase.senderid, config.firebase.serverkey);
obj.firebase = require('./firebase').CreateFirebase(obj, config.firebase.senderid, config.firebase.serverkey);
}
// Start periodic maintenance

View File

@ -407,8 +407,7 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
parent.db.Get(nodeid, function (err, nodes) {
if ((err == null) && (nodes != null) && (nodes.length == 1) && (typeof nodes[0].pmt == 'string')) {
if ((parent.GetNodeRights(obj.user, nodes[0].meshid, nodes[0]._id) & MESHRIGHT_CHATNOTIFY) != 0) {
obj.pmt = nodes[0].pmt;
obj.nodename = nodes[0].name;
obj.node = nodes[0];
// Create the peer connection URL, we will include that in push messages
obj.msgurl = req.headers.origin + (req.url.split('/.websocket')[0].split('/meshrelay.ashx').join('/messenger')) + '?id=' + req.query.id
}
@ -486,12 +485,12 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
command.title = (domain.title ? domain.title : 'MeshCentral');
var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } };
var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High'
parent.parent.firebase.sendToDevice(obj.pmt, payload, options, function (id, err, errdesc) {
parent.parent.firebase.sendToDevice(obj.node, payload, options, function (id, err, errdesc) {
if (err == null) {
parent.parent.debug('email', 'Successfully send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg);
parent.parent.debug('email', 'Successfully send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg);
try { ws.send(JSON.stringify({ action: 'ctrl', value: 1 })); } catch (ex) { } // Push notification success
} else {
parent.parent.debug('email', 'Failed to send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc);
parent.parent.debug('email', 'Failed to send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc);
try { ws.send(JSON.stringify({ action: 'ctrl', value: 2 })); } catch (ex) { } // Push notification failed
}
});

View File

@ -5207,7 +5207,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
// Send out a push message to the device
var payload = { notification: { title: command.title, body: command.msg } };
var options = { priority: "Normal", timeToLive: 5 * 60 }; // TTL: 5 minutes
parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) {
parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) {
if (err == null) {
parent.parent.debug('email', 'Successfully send push message to device ' + node.name + ', title: ' + command.title + ', msg: ' + command.msg);
} else {
@ -5229,12 +5229,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
const node = nodes[0];
if ((parent.GetNodeRights(user, node.meshid, node._id) == MESHRIGHT_ADMIN) && (typeof node.pmt == 'string')) {
// Send out a push message to the device
var payload = { data: { console: command.console, session: ws.sessionId } };
var payload = { data: { con: command.console, s: ws.sessionId } };
var options = { priority: "Normal", timeToLive: 60 }; // TTL: 1 minutes, priority 'Normal' or 'High'
parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) {
if (err == null) {
try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'OK' })); } catch (ex) { }
} else {
parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) {
if (err != null) {
try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'Failed: ' + errdesc })); } catch (ex) { }
parent.parent.debug('email', 'Failed to send push console message to device ' + node.name + ', command: ' + command.console + ', error: ' + errdesc);
}

View File

@ -9391,7 +9391,7 @@
t = '<div style=color:violet>' + "PUSH" + '&gt; ' + EscapeHtml(v) + '<br/></div>';
consoleNode.consoleText += t;
meshserver.send({ action: 'pushconsole', nodeid: consoleNode._id, console: v });
} else if ((consoleNode.conn & 1) == 0) {
} else if ((consoleNode.conn & 1) != 0) {
// Send the command to the mesh agent
consoleNode.consoleText += t;
meshserver.send({ action: 'msg', type: 'console', nodeid: consoleNode._id, value: v });