I use this library in the description that says I can get the following answers from the server.

GCM response

{ "multicast_id": 216, "success": 3, "failure": 3, "canonical_ids": 1, "results": [ { "message_id": "1:0408" }, { "error": "Unavailable" }, { "error": "InvalidRegistration" }, { "message_id": "1:1516" }, { "message_id": "1:2342", "registration_id": "32" }, { "error": "NotRegistered"} ] } 

module response

 { "multicast_id": 216, "success_length": 3, "failures_length": 3, "failures": { "NotRegistered": ["42"], "Unavailable": ["8"], "InvalidRegistration": ["15"] }, "canonical_ids_length": 1, "canonical_ids": [ { "message_id": "1:2342", "registration_id": "23", "new_registration_id": "32" } ] } 

Simple (plain-text response) GCM response

 id=1:2342 registration_id=32 

module response

 { "id": "1:2342", "registration_id": "32" "old_registration_id": "14" } 

Question: how can I extract the answer if I send the request in this way

  sender.sendMessage(message.toString(), token, false, function(err, data) { if (!err) { console.log(data) } else { console.log(err) } }); 

and in data , either the { id: '0:1462523393672289]ee2b8a002eefaa' } line is returned to me if the user's token is valid or { NotRegistered } if the user's token is invalid

    1 answer 1

    Made a request like this:

     var request = require('request'); var reg_id = 'MyRegistration_id'; var messageBody = { "collapse_key": "data", "time_to_live": 1000, "delay_while_idle": false, "data": { "message": "Http request test message", }, "to" : reg_id }; var options = { url: 'https://android.googleapis.com/gcm/send', json: true, method: "POST", headers: { 'Authorization': 'key=MyCustomApiKey', 'Content-Type': 'application/json' // use for plain/text data // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, body: messageBody }; function callback(error, response, body) { console.log('Response code : ' + response.statusCode) if (!error && response.statusCode == 200) { console.log("Response body: " + JSON.stringify(body)) } if (error != null){ console.log(error) } } request(options, callback);