Ionic Push REST Api Example?

Hi!
I tried to implement the new push service and it’s working so far!!!
But now I’m struggling with the REST Api to send notifications from the app / a server.

I read http://docs.ionic.io/v1.0/docs/push-sending-push but I’m not sure what to do with this information. Is it possible to make such a POST with PHP or are there any examples/tutorials where I can see how to configure this?

Thank you very much!

Can’t anyone help me with this? I think this is one of the main things what a developer wants to do with the push service, because only on this way a user can get notified from another. Or not?

Hi suggest you first try the curl examples that are in the previous section of the doc: http://docs.ionic.io/v1.0/docs/push-testing

Those are simple command that makes the required POST request to the ionic push service that in turns sends the real notification through GCM or APN services.

Were you able to register your client and get a device token? If so you can try to send a push substituting the parameters in this command right away:

curl -u PRIVATE_API_KEY: -H “Content-Type: application/json” -H “X-Ionic-Application-Id: APP_ID” https://push.ionic.io/api/v1/push -d ‘{“tokens”:[“DEVICE_TOKEN”],“notification”:{“alert”:“I come from planet Ion.”}}’

PRIVATE_API_KEY: the private key parameter configured in your apps.ionic.io application.
APP_ID: the id of your apps.ionic.io application
DEVICE_TOKEN: the token received during the registration process of the client. This is the most critical piece of data.

After you manage you to successfully send a push with curl you can easily construct the same POST request with PHP and therefore send the push with your backend app.
Sorry but I don’t have a PHP example to show you, I use python on my backend.

1 Like

Yes, I was able to register a client and got a device token. I was also able to send and receive a push notification via the CLI option ‘ionic push -s’.
But the problem ist still how to get this done with PHP.

In the example is the python-code, but I didn’t get the CURL command running with PHP.

My first steps:

    <?php
$url = 'https://push.ionic.io/api/v1/push';
$myjson = '{"tokens":["DEVICE_TOKEN"],"notification":{"alert":"I come from planet Ion."}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);

?>

The problem seems to be the headers and the Basic Access Authentication with the private API key.

Thank you very much so far!

1 Like

Of course headers are key.
To copy exactly the curl command that I pasted before, you should add something like:

curl_setopt($ch CURLOPT_USERPWD, PRIVATE_API_KEY . “:”);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘X-Ionic-Application-Id: APP_ID’,
‘Content-Type: application/json’
));

Obviously substituting correct values for PRIVATE_API_KEY and APP_ID.
You’ll also need to substitude the actual value of the token in DEVICE_TOKEN.
I’m no way an expert in PHP (just googled the commands) and I did not tested anything but that should be what you are missing.

1 Like

Ok, thank you very much! After some other smaller problems here now my working code, if anyone else maybe want to use it!

<?php
$msg = $_GET['msg'];
$user = $_GET['user'];

$device_token="TOKEN";


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

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_USERPWD, "Private Api key" . ":" );  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Ionic-Application-Id: App-ID' 
));
$result = curl_exec($ch);
curl_close($ch);

?>
3 Likes

Hi @basti, i use ionic.io but i cant get the device token. Could you help me how to get it?
Thank you

I’m also no pro, but doing it exactly how it’s described in the docs: http://docs.ionic.io/v1.0/docs/push-from-scratch worked very well for me, so give it a try maybe with the demo code also given there!

Thanks basti, i was looking for this server side part and work perfect in development, now i have two doubts.

1st: how can i made it works with google cloud message i had followed this tutorial

I can register the device and get the token but when i send the push to that token nothing happen.

2nd: i want to know how can i make the app stay listening for new pushes even if is closed like whatsapp and many other apps.

Thanks if somebody can help me.

I also just followed this example and unfortunately have no idea where the problem can be. One time I forgot to send my google api key to ionic, then it won’t work naturally;

ionic push --google-api-key your-google-api-key

If you pushed it to ionic you can also send one-time messages through the ionic.io backend under “Push”. There you also can see the log if the push was done correctly and search after single messages.

2nd: The app should receive pushes from google even it’s closed.

Man Thanks a lot for the code ! Was struggling for the past couple days!
I just copy/paste and it worked just fine.
Now let’s have some fun notifying everyone !

How to send the same push notification to more than one device token at a time?
(in PHP code)

I stored the tokens in a db and then fetched them:

<?php

$msg = $_GET['msg'];
$user = $_GET['user'];

// DB-Verbindung
include("dbconnect.php");

// Daten von DB holen
$sql = mysql_query("SELECT token,user FROM app_token");

$tokens=array();
while($row = mysql_fetch_row($sql))    
        if ($user!=$row[1] ) array_push($tokens, $row[0]); 

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

$data = array(
                  'tokens' => $tokens, //array($device_token), 
                  'notification' => array('alert' => ''.$user.': '.$msg),
                  //'android' => array('timeToLive' => 300, 'collapseKey' => "foo", 'delayWhileIdle'=>true,),
                 
                  );
      
$content = json_encode($data);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_USERPWD, "XXXXX" . ":" );  //Private Api key
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Ionic-Application-Id: 5db047d0' //App-ID
));
$result = curl_exec($ch);
curl_close($ch);

?>

I need your help :slight_smile: I follow all the instructions and every push notification that i send at API page ( $url = ‘https://api.ionic.io/push/notifications’; ) goes in 201… Always {“meta”: {“status”: 201, “request_id”:
“3c10ffb6-903f-49d6-81ec-11fe7af082ec”, “version”: “2.0.0-beta.0”},
“data”: {“status”: “open”, “uuid”:
“7cb82e5a-0786-476b-97bd-5a17f9ab6dc8”, “state”: “enqueued”, …

What can i do? nothing is recived, but if i try with same token device from web notification appear… HELP ME pls!

Hi, I had the same problem, did you try to add a priority attribute to your push request ?

I have created a simple library that allows you to consume the Ionic Cloud API for sending push notifications (normal and scheduled), get a paginated list of sending push notifications, get information of registered devices, remove registered devices by token, …

This library requires PHP 5.1+ and cURL.

You can read more about this library here:tomloprod/ionic-push-php