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

unable to send to topic #146

Closed sattha closed 9 years ago

sattha commented 9 years ago

because registerIDs is required and cannot empty or null

hypesystem commented 9 years ago

Hi! This is actually a question I have received before, and I think we should update the README to fit the truth. I will leave this issue open until this happens.

There is a simple way to do what you want. Simply pass the topic instead of the registration ids:

sender.send(message, "/some/topic", callback);

This puts "/some/topic" in the to field of the message JSON.

bastienleonard commented 9 years ago

@hypesystem:

I can't get your example to work with send(). It seems to come from this line: https://github.com/ToothlessGear/node-gcm/blob/master/lib/sender.js#L124. sendNoRetry() worked fine.

To the people who would try to copy-paste the example like me: don't use '/some/topic', topic names have to start with /topics/!

hypesystem commented 9 years ago

Ah! You are correct! It only works with sendNoRetry at the moment.

Another reason for leaving this issue open:

hypesystem commented 9 years ago

@bastienleonard I would love a PR if you have the time :)

bastienleonard commented 9 years ago

@hypesystem I'll try to do that after work!

hypesystem commented 9 years ago

Just had a thought: changing the interface would be a breaking change, and we are not quite ready to release anything called "1.0" yet.

I would prefer if you, in the PR, could make a new method on the Sender called sendTo (or similar) which works with topics/notification keys (things that go in the to field) :)

bastienleonard commented 9 years ago

Right, not converting to an array would break compatibility for some users.

How do you feel about send() accepting an explicit object, e.g. { "topic": "/topics/global" }? Topics and registration IDs being passed in the same JSON key to GCM seems like an implementation detail that shouldn't necessarily be present in the Node API.

hypesystem commented 9 years ago

I like that solution for not breaking compatibility :) go ahead!

tonespy commented 9 years ago

I get a 400 whenever I try to use the topic section. Or, must I pass ids when sending a push to the /topics which I don't think so. Since I get a 400 and that according to https://developers.google.com/cloud-messaging/http-server-ref#table1 means the data is not well structured and this is my data being sent too

var message = new gcm.Message({delayWhileIdle: true, priority: 'high', notification:{title:'Request', icon: 'ic_launcher', body: successData[0].Status}});

sendGlobalPush: function(message){ sender.sendNoRetry(message, {topic: '/topics/global'}, function(err, result){ if (err) { console.log(err); } else { console.log(result); } });

Any help. Thanks @hypesystem @bastienleonard

hypesystem commented 9 years ago

The topic support is not yet released, but will be in node-gcm v.0.12

tonespy commented 9 years ago

Ahn, but it's in the README file

// Send to a topic, with no retry this time sender.sendNoRetry(message, { topic: '/topics/global' }, function (err, result) { if(err) console.error(err); else console.log(result); });

hypesystem commented 9 years ago

The README file is in sync with the master branch. Please refer to the README on npmjs.org for the most recent released one.

hypesystem commented 9 years ago

This has now been released in 0.12.0