ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

yes i used node-gcm #159

Closed imvishalsingh closed 8 years ago

imvishalsingh commented 8 years ago

my server.js file code following

23 line set include gcm and set the api key 233 line start my send message function in 264 line number set the message body and perform the send function

var loopback = require('loopback'); var boot = require('loopback-boot');

var app = module.exports = loopback();

// Sub-apps like REST API are mounted via boot scripts. boot(app, __dirname);

app.start = function() { // start the web server return app.listen(function() { app.emit('started'); console.log('Web server listening at: %s', app.get('url')); }); };

var gcm = require('node-gcm');

var message = new gcm.Message();

// Set up the sender with you API key var sender = new gcm.Sender('MY Api key here');

var users = {}; var company = {}; var agent = {}; var msg = {}; var agentobj = {}; var lastagentid; var staticnew; var agentblock; // Bootstrap the application, configure models, datasources and middleware. // Sub-apps like REST API are mounted via boot scripts. // start the server if $ node server.js if (require.main === module) { //app.start(); app.io = require('socket.io')(app.start());

app.io.on('connection', function(socket){
console.log('a user connected: '+socket.id);
app.io.to(socket.id).emit('newsocket_id', socket.id);//send msg to agent

 socket.on('one client online', function(clientinfo){
 var clientobj = {"socket_id":socket.id, "ip":clientinfo.ip,"company_id":clientinfo.comp_id, "name":clientinfo.name, "depart":clientinfo.depart, "browsername":clientinfo.browsername,"os":clientinfo.os,"location":clientinfo.location};
 users[socket.id]= clientobj;
 console.log('a client online Ip:'+clientinfo.ip+' Socket_id:'+socket.id+' company_id:'+clientinfo.comp_id+' name:'+clientinfo.name+' Department:'+clientinfo.depart+' Browsername:'+clientinfo.browsername+' Os:'+clientinfo.os+' location:'+clientinfo.location+'');
 var staticold = 5000;
 var find_status = 'no';
 agentobj = company[clientinfo.comp_id];
  //nobody online it this point
  if(!agentobj)
  {
    msg.msg          = 'Sorry no agent online now, enter your email for further contact';
    msg.agent_status = 'noonline';
    msg.my_socket_id =  socket.id;
    app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
  }
 if(company[clientinfo.comp_id])
 {

 agentobj = company[clientinfo.comp_id];
 for (var key in agent){
    if (agent.hasOwnProperty(key)) {
     var depart = (agent[key].depart);

     var arr   = depart;             //agent department
     var check = clientinfo.depart;  //client  department

      var value = -1;
      for (var i = 0; i < check.length; i++) {
      if (arr.indexOf(check[i]) > -1) {
      value = 1;
      break;
      }
      }

    var gcm_tokkenid;

     if(value==-1)
     {
       console.log('agent not found');
       msg.msg          = 'Sorry no agent online now, enter your email for further contact';
       msg.agent_status =  'noonline';
       msg.my_socket_id =  clientobj.socket_id;
       //if not department agent found in this folder
       app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
     }
     else
     {

       gcm_tokkenid  = agent[key].gcm_tokkent;
        staticnew = (agent[key].chatcount);
        agentblock2 = (agent[key]);
        //console.log(agentblock2);
        if(staticold > staticnew)
        {
         staticold = staticnew;
         agentblock = agentblock2;
         find_status = 'yes';
        }
     }
    }
   }
   if(find_status == 'yes')
   {

   agent_id = agentblock.id;
   agentblock_new_count = (agentblock.chatcount) + 1;

   agent[agent_id] = {"socket_id":agentblock.socket_id,"id":agentblock.id,"name":agentblock.name,"depart":agentblock.depart,"chatcount":agentblock_new_count,"gcm_tokken":agentblock.gcm_tokken};

   //send message to client
   msg.gmc_tokken   = gcm_tokkenid
   msg.socket_id    = agentblock.socket_id;
   msg.agent_id     = agentblock.id;
   msg.name         = agentblock.name;
   msg.id           = agentblock.id;
   msg.agent_status =  'online';
   msg.msg          = 'Hello ! May I help you ?';
   msg.my_socket_id =  clientobj.socket_id;

    //start a new conversation (create new conversation)

    var clientInfo = {"browsername":clientinfo.browsername,"os":clientinfo.os,"myip":clientinfo.ip,"city":clientinfo.location,"my_socketid":socket.id};
    app.models.Conversation.create({"startTime":currenttime(), "endTime":"null","agentId":msg.agent_id,"date":currentdate(),"clientName":clientobj.name,"clientInfo":clientInfo},function(err, data){
    clientobj.conversation_id = data.id;
    msg.conversation_id       = data.id;
    clientobj.agent_socket_id = agentblock.socket_id;
    app.io.to(agentblock.socket_id).emit('first_call',clientobj);//send msg to agent
    app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
    console.log('message send to client'+JSON.stringify(msg.name));
    });

     /*current date function*/
    function currentdate()
    {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    if(dd<10) {
    dd='0'+dd
    }
    if(mm<10) {
    mm='0'+mm
    }
    today = dd+'/'+mm+'/'+yyyy;
    return today;
    }

     /*current time function*/
    function currenttime()
    {
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes
    var time = (hours+':'+minutes);
    return time;
    }

   }
   else
   {

   }
 }
 else
 {

 }

 });

 socket.on('one agent online', function(agentinfo,clientinfo){
 if(lastagentid != agentinfo.comp_id)
 {
   if(lastagentid)
   {
   if(company[agentinfo.comp_id])
   {
     agent = company[agentinfo.comp_id];
   }
   else
   {
     agent = {};
   }
   }
 }
 lastagentid = agentinfo.comp_id;
 agent[agentinfo.id]={"socket_id":socket.id,"company_id":agentinfo.comp_id, "id":agentinfo.id,"name":agentinfo.agentname,"depart":agentinfo.depart,"chatcount":0,"gcm_tokkent":agentinfo.gcm_tokken};
 company[agentinfo.comp_id] = agent;
 console.log('a agent online socket_id:'+socket.id+' company_id:'+agentinfo.comp_id+' id:'+agentinfo.id+' name:'+agentinfo.agentname+' depart:'+agentinfo.depart+' Chatcount:0 gcm_tokkent:'+agentinfo.gcm_tokken);
 });

  //start new conversataion when agent offline
  socket.on('start_conversation',function(msgblock){
  clientInfo = {"browsername":msgblock.browsername,"os":msgblock.os,"myip":msgblock.myip,"city":msgblock.city,"my_socketid":msgblock.mysocket_id};
  app.models.Conversation.create({"startTime":currenttime(), "endTime":"null", "agentId": 000,"clientName":msgblock.client_name,"clientEmail":msgblock.client_email,"lastMessageby":'client',"clientInfo":clientInfo,"departmentRequest":msgblock.Department,"orgnisationId":msgblock.companyid},function(err, data){
  app.io.to(msgblock.mysocket_id).emit('conversaton_id', data);//send msg to agent
  })

   function currenttime()
   {
   var currentTime = new Date()
   var hours = currentTime.getHours()
   var minutes = currentTime.getMinutes()

   if (minutes < 10)
   minutes = "0" + minutes
   var time = (hours+':'+minutes);
   return time;
   }
  });

  //exchange the two way message
  socket.on('new message',function(msgblock){

  if(msgblock.client_email)
  {
   app.models.Messages.create({"agentId": 000, "text": msgblock.msg, "msgtype":'client', "customerName": msgblock.client_name, "timestamp": currenttime(), "converstionId": msgblock.conversation_id},function(err, data){
    console.log('Message successful save');
  })
  }
  else
  {
 app.models.Messages.create({"agentId": msgblock.agent_id, "text": msgblock.msg, "msgtype":msgblock.msgtype, "customerName": msgblock.client_name, "timestamp": currenttime(), "converstionId": msgblock.conversation_id},function(err, data){
 if(msgblock.sendby == 'client')
 {
   agentgroup  = company[msgblock.companyid];
   agentobject = agentgroup[msgblock.agent_id];

   app.io.to(agentobject.socket_id).emit('new message', msgblock);//send msg to agent
   console.log('message send to socket_id = '+agentobject.socket_id+' and message are '+msgblock.msg+'');

   var regIds = msgblock.gcm_tokken;

  // ... or some given values
  var message = new gcm.Message({
      collapseKey: 'demo',
      priority: 'high',
      contentAvailable: true,
      delayWhileIdle: true,
      timeToLive: 2,
      restrictedPackageName: "somePackageName",
      dryRun: true,
      data: {
          key1: 'message1',
          key2: 'message2'
      },
      notification: {
          title: "Hello, World",
          icon: "ic_launcher",
          body: "This is a notification that will be displayed ASAP."
      }
  });

  // Send the message
  // ... trying only once
  sender.send(message, { registrationIds: regIds }, function(err, result) {
    if(err) console.error(err);
    else    console.log(result);
  });

 }
 else
 {
   app.io.to(msgblock.socket_id).emit('new message', msgblock);//send msg to client
   console.log('message send to socket_id = '+msgblock.socket_id+' and message are '+msgblock.msg+'');
 }

 });
}

 function currenttime()
 {
 var currentTime = new Date()
 var hours = currentTime.getHours()
 var minutes = currentTime.getMinutes()

 if (minutes < 10)
 minutes = "0" + minutes
 var time = (hours+':'+minutes);
 return time;
 }
 })

 //refresh the agent listing
 socket.on('refresh_agentlist',function(agentobject){
 agentgroup     = company[agentobject.company_id];
 agentobj       = agentgroup[agentobject.myid];
 agentsocket_id = agentobj.socket_id;
 app.io.to(agentsocket_id).emit('refresh_Agent_list', agentobject);//send msg to agent
 });

 //mail to client
 socket.on('mail_To_user',function(object,company){
   app.models.Email.send({
    to: object.clientEmail,
    from: object.clientEmail,
    subject: 'Chat Reply',
    html: '<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse;background-color:#f5f8fa;text-align:center;"><tr><td align="center" class="framepadding" style="padding-left:30px;padding-right:30px;"><table class="frame" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse;background-color:#f5f8fa;width:600px;min-width:320px;text-align:center;" align="center"><tr><td style="font-size:15px;font-family:Arial,sans-serif;line-height:18px;"><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0" align="left" style="border-collapse:collapse;background-color:#55acee;text-align:left;"><tr><td><h2 style="color:#fff">&nbsp;&nbsp;&nbsp;'+company+'</h2></td></tr></table></td></tr><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff" align="left" style="border-collapse:collapse;text-align:left;"><tr><td class="bodypadding" style="padding:30px;font-size:15px;font-family:Arial,sans-serif;line-height:18px;color:#66757f;"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff" align="left" style="border-collapse:collapse;text-align:left;"><tr>Hello '+object.clientName+',<br><br>Feeling confused by DMs and RTs? Need to go back to basics with Twitter? If so, then we have the perfect solution for you, our The Basics of Twitter for business webinar.<br><br><a href="https://twitter.twimg.com/TwitterAcademy?utm_campaign=AQ_WC_SMB_WebinarPromotion_Nordics_EN_141008_Invite2&utm_medium=email&utm_source=Eloqua&elq=eed72069907047b697e1a451b00a9b05&elqCampaignId=1963" style="text-decoration:none;font-weight:bold;color:#55acee;">Join us</a> for 30 mins of essential Twitter info, including:<ul><li>A beginner’s overview on how to navigate Twitter</li><li>Our 5 basic tips for success on Twitter</li><li>A few handy tools for planning content and managing your Twitter account</li></ul><center><br><br><div><a href="http://localhost/client_offlinechatbox/index.html?convertion_id='+object.id+'" style="background-color:#55acee;border-radius:5px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:15px;font-weight:bold;line-height:45px;text-align:center;text-decoration:none;width:200px;-webkit-text-size-adjust:none;">Open Chatbox</a></div></center></td></tr></table></td></tr></table></td></tr><tr><td></td></tr><tr><td bgcolor="#f5f8fa">&nbsp;</td></tr></table></td></tr></table>'

  }, function(err, mail) {
    console.log('email sent!');
    console.log(err);
  });
 });

   socket.on('disconnect user',function(agent_id, company_id){
   agentobj = company[company_id];
   delete agentobj[agent_id];
   company[company_id] = agentobj;
   console.log('A Agent offline now');
   });

    socket.on('disconnect_chat',function(object){
    app.models.Conversation.updateAll({"id":object.conversation_id},{"endTime":currenttime()}, function(err, info) {
    app.io.to(object.socket_id_my).emit('connection_close', object);//send msg to agent
    app.io.to(object.socket_id_clident).emit('connection_close', object);//send msg to client
    });

   function currenttime()
   {
   var currentTime = new Date()
   var hours = currentTime.getHours()
   var minutes = currentTime.getMinutes()

   if (minutes < 10)
   minutes = "0" + minutes
   var time = (hours+':'+minutes);
   return time;
   }

 })

});

}

var loopback = require('loopback'); var boot = require('loopback-boot');

var app = module.exports = loopback();

// Sub-apps like REST API are mounted via boot scripts. boot(app, __dirname);

app.start = function() { // start the web server return app.listen(function() { app.emit('started'); console.log('Web server listening at: %s', app.get('url')); }); };

var gcm = require('node-gcm');

var message = new gcm.Message();

// Set up the sender with you API key var sender = new gcm.Sender('AIzaSyD91AnA5zYRc-t621tkAAK8Gl2OSfOQwmo');

var users = {}; var company = {}; var agent = {}; var msg = {}; var agentobj = {}; var lastagentid; var staticnew; var agentblock; // Bootstrap the application, configure models, datasources and middleware. // Sub-apps like REST API are mounted via boot scripts. // start the server if $ node server.js if (require.main === module) { //app.start(); app.io = require('socket.io')(app.start());

app.io.on('connection', function(socket){
console.log('a user connected: '+socket.id);
app.io.to(socket.id).emit('newsocket_id', socket.id);//send msg to agent

 socket.on('one client online', function(clientinfo){
 var clientobj = {"socket_id":socket.id, "ip":clientinfo.ip,"company_id":clientinfo.comp_id, "name":clientinfo.name, "depart":clientinfo.depart, "browsername":clientinfo.browsername,"os":clientinfo.os,"location":clientinfo.location};
 users[socket.id]= clientobj;
 console.log('a client online Ip:'+clientinfo.ip+' Socket_id:'+socket.id+' company_id:'+clientinfo.comp_id+' name:'+clientinfo.name+' Department:'+clientinfo.depart+' Browsername:'+clientinfo.browsername+' Os:'+clientinfo.os+' location:'+clientinfo.location+'');
 var staticold = 5000;
 var find_status = 'no';
 agentobj = company[clientinfo.comp_id];
  //nobody online it this point
  if(!agentobj)
  {
    msg.msg          = 'Sorry no agent online now, enter your email for further contact';
    msg.agent_status = 'noonline';
    msg.my_socket_id =  socket.id;
    app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
  }
 if(company[clientinfo.comp_id])
 {

 agentobj = company[clientinfo.comp_id];
 for (var key in agent){
    if (agent.hasOwnProperty(key)) {
     var depart = (agent[key].depart);

     var arr   = depart;             //agent department
     var check = clientinfo.depart;  //client  department

      var value = -1;
      for (var i = 0; i < check.length; i++) {
      if (arr.indexOf(check[i]) > -1) {
      value = 1;
      break;
      }
      }

    var gcm_tokkenid;

     if(value==-1)
     {
       console.log('agent not found');
       msg.msg          = 'Sorry no agent online now, enter your email for further contact';
       msg.agent_status =  'noonline';
       msg.my_socket_id =  clientobj.socket_id;
       //if not department agent found in this folder
       app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
     }
     else
     {

       gcm_tokkenid  = agent[key].gcm_tokkent;
        staticnew = (agent[key].chatcount);
        agentblock2 = (agent[key]);
        //console.log(agentblock2);
        if(staticold > staticnew)
        {
         staticold = staticnew;
         agentblock = agentblock2;
         find_status = 'yes';
        }
     }
    }
   }
   if(find_status == 'yes')
   {

   agent_id = agentblock.id;
   agentblock_new_count = (agentblock.chatcount) + 1;

   agent[agent_id] = {"socket_id":agentblock.socket_id,"id":agentblock.id,"name":agentblock.name,"depart":agentblock.depart,"chatcount":agentblock_new_count,"gcm_tokken":agentblock.gcm_tokken};

   //send message to client
   msg.gmc_tokken   = gcm_tokkenid
   msg.socket_id    = agentblock.socket_id;
   msg.agent_id     = agentblock.id;
   msg.name         = agentblock.name;
   msg.id           = agentblock.id;
   msg.agent_status =  'online';
   msg.msg          = 'Hello ! May I help you ?';
   msg.my_socket_id =  clientobj.socket_id;

    //start a new conversation (create new conversation)

    var clientInfo = {"browsername":clientinfo.browsername,"os":clientinfo.os,"myip":clientinfo.ip,"city":clientinfo.location,"my_socketid":socket.id};
    app.models.Conversation.create({"startTime":currenttime(), "endTime":"null","agentId":msg.agent_id,"date":currentdate(),"clientName":clientobj.name,"clientInfo":clientInfo},function(err, data){
    clientobj.conversation_id = data.id;
    msg.conversation_id       = data.id;
    clientobj.agent_socket_id = agentblock.socket_id;
    app.io.to(agentblock.socket_id).emit('first_call',clientobj);//send msg to agent
    app.io.to(socket.id).emit('welcome_msg', msg);//send msg to client
    console.log('message send to client'+JSON.stringify(msg.name));
    });

     /*current date function*/
    function currentdate()
    {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    if(dd<10) {
    dd='0'+dd
    }
    if(mm<10) {
    mm='0'+mm
    }
    today = dd+'/'+mm+'/'+yyyy;
    return today;
    }

     /*current time function*/
    function currenttime()
    {
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes
    var time = (hours+':'+minutes);
    return time;
    }

   }
   else
   {

   }
 }
 else
 {

 }

 });

 socket.on('one agent online', function(agentinfo,clientinfo){
 if(lastagentid != agentinfo.comp_id)
 {
   if(lastagentid)
   {
   if(company[agentinfo.comp_id])
   {
     agent = company[agentinfo.comp_id];
   }
   else
   {
     agent = {};
   }
   }
 }
 lastagentid = agentinfo.comp_id;
 agent[agentinfo.id]={"socket_id":socket.id,"company_id":agentinfo.comp_id, "id":agentinfo.id,"name":agentinfo.agentname,"depart":agentinfo.depart,"chatcount":0,"gcm_tokkent":agentinfo.gcm_tokken};
 company[agentinfo.comp_id] = agent;
 console.log('a agent online socket_id:'+socket.id+' company_id:'+agentinfo.comp_id+' id:'+agentinfo.id+' name:'+agentinfo.agentname+' depart:'+agentinfo.depart+' Chatcount:0 gcm_tokkent:'+agentinfo.gcm_tokken);
 });

  //start new conversataion when agent offline
  socket.on('start_conversation',function(msgblock){
  clientInfo = {"browsername":msgblock.browsername,"os":msgblock.os,"myip":msgblock.myip,"city":msgblock.city,"my_socketid":msgblock.mysocket_id};
  app.models.Conversation.create({"startTime":currenttime(), "endTime":"null", "agentId": 000,"clientName":msgblock.client_name,"clientEmail":msgblock.client_email,"lastMessageby":'client',"clientInfo":clientInfo,"departmentRequest":msgblock.Department,"orgnisationId":msgblock.companyid},function(err, data){
  app.io.to(msgblock.mysocket_id).emit('conversaton_id', data);//send msg to agent
  })

   function currenttime()
   {
   var currentTime = new Date()
   var hours = currentTime.getHours()
   var minutes = currentTime.getMinutes()

   if (minutes < 10)
   minutes = "0" + minutes
   var time = (hours+':'+minutes);
   return time;
   }
  });

  //exchange the two way message
  socket.on('new message',function(msgblock){

  if(msgblock.client_email)
  {
   app.models.Messages.create({"agentId": 000, "text": msgblock.msg, "msgtype":'client', "customerName": msgblock.client_name, "timestamp": currenttime(), "converstionId": msgblock.conversation_id},function(err, data){
    console.log('Message successful save');
  })
  }
  else
  {
 app.models.Messages.create({"agentId": msgblock.agent_id, "text": msgblock.msg, "msgtype":msgblock.msgtype, "customerName": msgblock.client_name, "timestamp": currenttime(), "converstionId": msgblock.conversation_id},function(err, data){
 if(msgblock.sendby == 'client')
 {
   agentgroup  = company[msgblock.companyid];
   agentobject = agentgroup[msgblock.agent_id];

   app.io.to(agentobject.socket_id).emit('new message', msgblock);//send msg to agent
   console.log('message send to socket_id = '+agentobject.socket_id+' and message are '+msgblock.msg+'');

   var regIds = msgblock.gcm_tokken;

  // ... or some given values
  var message = new gcm.Message({
      collapseKey: 'demo',
      priority: 'high',
      contentAvailable: true,
      delayWhileIdle: true,
      timeToLive: 2,
      restrictedPackageName: "somePackageName",
      dryRun: true,
      data: {
          key1: 'message1',
          key2: 'message2'
      },
      notification: {
          title: "Hello, World",
          icon: "ic_launcher",
          body: "This is a notification that will be displayed ASAP."
      }
  });

  // Send the message
  // ... trying only once
  sender.send(message, { registrationIds: regIds }, function(err, result) {
    if(err) console.error(err);
    else    console.log(result);
  });

 }
 else
 {
   app.io.to(msgblock.socket_id).emit('new message', msgblock);//send msg to client
   console.log('message send to socket_id = '+msgblock.socket_id+' and message are '+msgblock.msg+'');
 }

 });
}

 function currenttime()
 {
 var currentTime = new Date()
 var hours = currentTime.getHours()
 var minutes = currentTime.getMinutes()

 if (minutes < 10)
 minutes = "0" + minutes
 var time = (hours+':'+minutes);
 return time;
 }
 })

 //refresh the agent listing
 socket.on('refresh_agentlist',function(agentobject){
 agentgroup     = company[agentobject.company_id];
 agentobj       = agentgroup[agentobject.myid];
 agentsocket_id = agentobj.socket_id;
 app.io.to(agentsocket_id).emit('refresh_Agent_list', agentobject);//send msg to agent
 });

 //mail to client
 socket.on('mail_To_user',function(object,company){
   app.models.Email.send({
    to: object.clientEmail,
    from: object.clientEmail,
    subject: 'Chat Reply',
    html: '<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse;background-color:#f5f8fa;text-align:center;"><tr><td align="center" class="framepadding" style="padding-left:30px;padding-right:30px;"><table class="frame" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse;background-color:#f5f8fa;width:600px;min-width:320px;text-align:center;" align="center"><tr><td style="font-size:15px;font-family:Arial,sans-serif;line-height:18px;"><tr><td><table width="100%" border="0" cellspacing="0" cellpadding="0" align="left" style="border-collapse:collapse;background-color:#55acee;text-align:left;"><tr><td><h2 style="color:#fff">&nbsp;&nbsp;&nbsp;'+company+'</h2></td></tr></table></td></tr><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff" align="left" style="border-collapse:collapse;text-align:left;"><tr><td class="bodypadding" style="padding:30px;font-size:15px;font-family:Arial,sans-serif;line-height:18px;color:#66757f;"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff" align="left" style="border-collapse:collapse;text-align:left;"><tr>Hello '+object.clientName+',<br><br>Feeling confused by DMs and RTs? Need to go back to basics with Twitter? If so, then we have the perfect solution for you, our The Basics of Twitter for business webinar.<br><br><a href="https://twitter.twimg.com/TwitterAcademy?utm_campaign=AQ_WC_SMB_WebinarPromotion_Nordics_EN_141008_Invite2&utm_medium=email&utm_source=Eloqua&elq=eed72069907047b697e1a451b00a9b05&elqCampaignId=1963" style="text-decoration:none;font-weight:bold;color:#55acee;">Join us</a> for 30 mins of essential Twitter info, including:<ul><li>A beginner’s overview on how to navigate Twitter</li><li>Our 5 basic tips for success on Twitter</li><li>A few handy tools for planning content and managing your Twitter account</li></ul><center><br><br><div><a href="http://localhost/client_offlinechatbox/index.html?convertion_id='+object.id+'" style="background-color:#55acee;border-radius:5px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:15px;font-weight:bold;line-height:45px;text-align:center;text-decoration:none;width:200px;-webkit-text-size-adjust:none;">Open Chatbox</a></div></center></td></tr></table></td></tr></table></td></tr><tr><td></td></tr><tr><td bgcolor="#f5f8fa">&nbsp;</td></tr></table></td></tr></table>'

  }, function(err, mail) {
    console.log('email sent!');
    console.log(err);
  });
 });

   socket.on('disconnect user',function(agent_id, company_id){
   agentobj = company[company_id];
   delete agentobj[agent_id];
   company[company_id] = agentobj;
   console.log('A Agent offline now');
   });

    socket.on('disconnect_chat',function(object){
    app.models.Conversation.updateAll({"id":object.conversation_id},{"endTime":currenttime()}, function(err, info) {
    app.io.to(object.socket_id_my).emit('connection_close', object);//send msg to agent
    app.io.to(object.socket_id_clident).emit('connection_close', object);//send msg to client
    });

   function currenttime()
   {
   var currentTime = new Date()
   var hours = currentTime.getHours()
   var minutes = currentTime.getMinutes()

   if (minutes < 10)
   minutes = "0" + minutes
   var time = (hours+':'+minutes);
   return time;
   }

 })

});

}

hypesystem commented 8 years ago

Hi @vishaloban,

You have a lot of code and it is written quite messily, making it hard to give feedback. I would recommend that you try to live up to the DRY programming principle: Don't Repeat Yourself. You have the same code a lot of places, making it hard to maintain, let alone understand.

It looks like you have copied, ad verbatim, the overview of usage for node-gcm, without modifying the code for your purposes. Most interestingly, you have left in the field restrictedPackageName: "somePackageName", which means that only apps with the package name "restrictedPackageName" can receive the notification.

Basically: you should modify the example code before using it. Remember to use the correct API key and actual registration Ids.

I am closing this issue as there are many other issues with the code than just those regarding to node-gcm, and it will likely lead to more confusion than help for others.