Getting an error from Ionic Push API when trying to send a push notification

Hi,
I am trying to send a push notification through the Push API, as explained here: http://docs.ionic.io/v1.0/docs/push-sending-push

But the response I’m getting is always this:
{
“result”: “error”,
“message”: “Either a single or list of tokens is required”
}

My payload is this:
{
“tokens”:[
“b284a6f7545368d2d3f753263e3e2f2b7795be5263ed7c95017f628730edeaad”,
“d609f7cba82fdd0a568d5ada649cddc5ebb65f08e7fc72599d8d47390bfc0f20”
],
“notification”:{
“alert”:“Hello World!”,
“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”
}
}
}
}

I tried many variations: passing a single string instead of an array, passing real and fake tokens, passing dev and android tokens, passing just one token in the array, with out without the optional sections of “android” and “ios”.

I always get the error above.

Any ideas why?

I am having the same issue, and I am using a script that worked for me previously. Perhaps something has recently broken?

Hi, i’m using the Advanced Rest Client.

And works perfect to me

url: https://push.ionic.io/api/v1/push
headers:

X-Ionic-Application-Id: YOUR_APP_ID
Authorization: Basic YOUR_PRIVATE_API_KEY-BASE64ENCODED

payload:

{
  "tokens":[
    "DEV-cf6b7ff1-fc8c-436e-a5f7-a6bd8542699c",
    "DEV-92824417-0d4f-4f25-8027-52d12fbee431"
  ],
  "notification":{
    "alert":"Hello World!",
    "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"
      }
    }
  }
}

test this, if you need i have a php script who works perfect to me.

Hi Obenavidez,

May i have the PHP script that you are using to push notification which works for you?

Best Regards,
Desmond

sorry desBiz for the late this is my script

<?php
include("../conexion/conexion.inc.php");
$link = Conectarse();
error_reporting(0);

$sql = "select token_push from mo_usuario where estado = '1'";

$resultado=mysql_query($sql);
                
if ( !$resultado ) 
    { 
        exit( "Error en la consulta SQL" ); 
    }

$data=array();
$i=0;
while($row=mysql_fetch_array($resultado, MYSQL_ASSOC)){    
$data[] = $row['token_push'];
}

$msg = "Nuevas Promociones y Eventos";//$_GET['msg'];
$user = "MoveOn";//$_GET['user'];

$device_token=$data;



$url = 'https://push.ionic.io/api/v1/push';

$data = array(
                  'tokens' => $device_token, 
                  'notification' => array('alert' => ''.$user.': '.$msg),      
                  );
echo json_encode($data);
$content = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_USERPWD, "public-key-of-your-app" . ":" );  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Ionic-Application-Id: your-id-ionic-app' 
));
$result = curl_exec($ch);
//var_dump($result);
curl_close($ch);

?>

I had a similar problem when creating a request in C# using the RestSharp library.

This worked for me:

var privateKey = "xxxx"; var tokens = new String[] { "yyyy", "yyyy" }; var appId = "zzzz";
        dynamic jsonPostData = new
        {
            tokens = tokens,
            production = false,
            notification = new
            {
                alert = "Hello world",
                title = "test",
            }
        };

        var client = new RestClient("https://push.ionic.io/api/v1");
        client.Authenticator = new HttpBasicAuthenticator(privateKey, "");

        var request = new RestRequest("push", Method.POST);

        request.AddJsonBody(jsonPostData);

        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("X-Ionic-Application-Id", appId);

        var response = client.Execute(request);
        var content = response.Content;

The request has 5 parameters:

{application/json={“tokens”:[“yyyy”,“yyyy”],“production”:false,“notification”:{“alert”:“Hello world”,“title”:“test”}}}
{Content-Type=application/json}
{X-Ionic-Application-Id=zzzz}
{Authorization=Basic xxxx-encodedbase64}
{Accept=application/json, application/xml, text/json, text/x-json, text/javascript, text/xml}