mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-03-09 15:40:18 +00:00
Added device console push notification support.
This commit is contained in:
parent
2248cb0a6d
commit
bd17264b05
5 changed files with 30 additions and 17 deletions
24
firebase.js
24
firebase.js
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue