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.31k stars 206 forks source link

Should sender be instantiated with every new message? #124

Closed AdamBrodzinski closed 9 years ago

AdamBrodzinski commented 9 years ago

It's kind of hard to tell from the readme, Should I be instantiating the sender every time I create a new message? Or can I instantiate it once and call sender.send multiple times?

Thanks! Adam

hypesystem commented 9 years ago

You only need one sender per application you are sending notifications to, and you can send as many messages as you want from the sender. I agree that this could be clearer in the README.

I'll leave the issue open to have a look at improving the README.

AdamBrodzinski commented 9 years ago

Thanks for the clarification! I assumed this was the case but I wanted to double check I didn't create some kind of weird edge case bug :laughing:

I'm not sure if this makes it more clear for the overall example but here's an idea for the example application code block:


// setup a gcm server
//
var gcm = require('node-gcm');
var sender = new gcm.Sender('YOUR_API_KEY_HERE');

// function to send a push message
//
function sendPushMessage(opts) {
  var message = new gcm.Message();
  message.addData('title', opts.title);
  message.addData('message', opts.message);
  message.addData(opts.data);
  var regIds = opts.regIds;

  sender.send(message, regIds, function (err, result) {
    if(err) console.error(err);
    else    console.log(result);
  });
}

// send a test message
//
sendPushMessage({
  title: "Hello Title",
  message: 'Hello World',
  regIds: ['1234'],
  data:{key1: 'message1', key2: 'message2'}
});
hypesystem commented 9 years ago

The README has been improved.