Trying to reach Ionic Push Api with Node.js (Android)

Hi Guys, I’ve been stuck on this for quite a while now. I am trying to reach the Ionic push API over node js, I will probably deploy the server on Heroku. I have been searching through a lot of forums now, but I couldn’t find a good solution yet. There is a library for iOS which does what I want, but for Android I haven’t found anything.
If I paste my header, payload and the ionic url in Google’s advanced rest client, I receive a message on my device. So my post parameters seem to be okay.

But running my node.js file gives me the following error:

{“result”: “error”, “message”: “A list of tokens or user_ids is required”}

// node modules
var request = require('request');
var Buffer = require('buffer').Buffer;

// Ionic Access variables
var privateKey = 'myKey';
var tokens = "myDeviceToken";
var appId = 'myAppId';

// encode the key
var b = new Buffer(privateKey);
var auth = b.toString('base64');

// post headers
var headers = {
    'Content-Type': 'application/json',
    'X-Ionic-Application-Id': appId,
    'Authorization': 'basic ' + auth
};    

var payload = {
  "tokens":[
    tokens
  ],
  "notification":{
    "alert": "Hello it is working!!",
    "ios":{
      "badge":1,
      "sound":"ping.aiff",
      "expiry": 1423238641,
      "priority": 10,
      "contentAvailable": true,
      "payload":{
        "key1":"value",
        "key2":"value"
      }
    },
    "android":{
      "collapseKey":"foo",
      "delayWhileIdle":true,
      "timeToLive":300,
      "payload":{
        "key1":"value",
        "key2":"value"
      }
    }
  }
};
 
// Configure the request
var options = {
    url: 'https://push.ionic.io/api/v1/push',
    method: 'POST',
    headers: headers,
    form: payload
};
 
// Start the request
request(options, function (error, response, body) {
    console.log(body);
    if (!error && response.statusCode == 200) {
        // Print out the response body
        console.log(body)
    }
});

This is my response in detail:

1 Like

For anyone out there I’ve got it to work with the following:

// node modules
var http = require(‘http’);
var Buffer = require(‘buffer’).Buffer;
var inspect = require(‘eyespect’).inspector();

// Ionic Access variables
var privateKey = ‘myKey’;
var tokens = deviceToken’;
var appId = ‘appId’;

// encode the key
var b = new Buffer(privateKey);
var auth = b.toString(‘base64’);

// notification information
var payload = {
“tokens”:[
tokens
],
“notification”:{
“alert”: “Hello Marvin!”,
“ios”:{
“badge”:1,
“sound”:“ping.aiff”,
“expiry”: 1423238641,
“priority”: 10,
“contentAvailable”: true,
“payload”:{
“key1”:“value”,
“key2”:“value”
}
},
“android”:{
“collapseKey”:“foo”,
“delayWhileIdle”:true,
“timeToLive”:300,
“payload”:{
“key1”:“value”,
“key2”:“value”
}
}
}
};

// convert payload to json
var body = JSON.stringify(
payload
);

// send request
var request = new http.ClientRequest({
hostname: “push.ionic.io”,
port: 80,
path: “/api/v1/push”,
method: “POST”,
headers: {
“Content-Type”: “application/json”,
‘X-Ionic-Application-Id’: appId,
‘Authorization’: 'basic ’ + auth,
“Content-Length”: Buffer.byteLength(body)
}
});
request.end(body);

// log response message
request.on(‘response’, function (response) {
var data = ‘’;

response.on(‘data’, function (chunk) {
data += chunk;
});

response.on(‘end’, function(){
inspect(data, ‘message’);
});
});