jlcvp / fcm-node

A Node.JS simple interface to Google's Firebase Cloud Messaging (FCM) for Android & iOS & Web Notification and data push
MIT License
125 stars 46 forks source link

Send function always logs an error #1

Closed kabirgh closed 7 years ago

kabirgh commented 8 years ago

The following code always logs an error.

fcm.send(message, function(err, response){ if (err) { console.log("Something has gone wrong!"); } else { console.log("Successfully sent with response: ", response); } });

Printing err to console shows a JSON object of the format
{ "multicast_id": 78797070, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:nsgjkndklg" } ] }

Suggestion: fcm.send(message, function(jsonResponse){ var response = JSON.parse(jsonResponse); if (response["failure"] > 0) { console.log("Something has gone wrong!"); } else { console.log("Successfully sent"); } });

jlcvp commented 8 years ago

Thanks @kabir-plod for reporting this, I fixed it.

it was a problem with handling response messages. Version 1.0.6 is now available on npm. Now error object will only be present if the failure attribute is greater than zero.

minhuyen commented 7 years ago

On version 1.0.9 it still always log an error. I think you should comment if (data.indexOf('\"failure\":') > -1) { error = data.substring(0).trim(); } else if (data.indexOf('\"success\":') > -1) { id = data.substring(0).trim(); } else if (data.indexOf('Unauthorized') > -1) { error = 'NotAuthorizedError' } else if(data.indexOf('\"message_id\":')>-1){ //topic messages id=data; }else { error = 'InvalidServerResponse'; }

jlcvp commented 7 years ago

@minhuyen can you please specify the type of message you're trying to send ? is it a topic message, direct message or multicast ?

jlcvp commented 7 years ago

@minhuyen anyway I've pushed a little fix, can you test and see if it fixed this issue for you? Version is 1.0.10 now

minhuyen commented 7 years ago

@jlcvp I am trying both multicast and single recipients but it always response data as below: { "message": { "multicast_id": 8699248289294424000, "success": 1, "failure": 0, "canonical_ids": 0, "results": [ { "message_id": "0:1469589874014319%b0b44e3db0b44e3d" } ] } }

jlcvp commented 7 years ago

Ok, my handling of the response json is really shady, I'll clarify it and push an update soon

jlcvp commented 7 years ago

@minhuyen Google have changed some return messages and I refactored the parsing of result messages. Pushed a fix and tested in some scenarios, please update your version to 1.0.11.

I'll close this issue for now. If you continue experiencing this kind of issue, feel free to open another one (or reopen this one if this error come back).