Reputation: 6053
I have follow the all steps to built simple app in ionic framework from here.And I am using php code for server side to call the ionic API for push notification. I have Used the following code but i am not getting notification in my app , Please suggest the solution.
<?php
$androidAppId = "e2c77770";
$data = array(
"tokens" => "APA91bF2YePDKxE6K6vZYs2KQ27Z4mdehJg-EaZaPy10w-RHN5RUgC_P6Uie24Qu_M28j9bfZcbU6pu8Awofa8h2G5j9jABnebrVIUgKM5JcZPEJHYVW2NINirAm7VnSqGOrqm4YicAoI9Xiw5zkgTx4edqXIANLEhvqsqSCeq-_gAuzZB8wvrQ",
"notification" => "Hello World!"
);
$data_string = json_encode($data);
$ch = curl_init('https://push.ionic.io/api/v1/push');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Ionic-Application-Id: '.$androidAppId,
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
?>
Upvotes: 4
Views: 3540
Reputation: 7890
I have created a complete 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 notifications messages, get information of registered devices, remove registered devices by token, etc.
This library requires PHP 5.1+
and cURL
.
composer require tomloprod/ionic-push-php
use Tomloprod\IonicApi\Push;
$ionicPushApi = new Push($ionicProfile, $ionicAPIToken);
// Configuration of the notification
$notificationConfig = [
'title' => 'Your notification title',
'message' => 'Your notification message. Bla, bla, bla, bla.'
];
// [OPTIONAL] You can also pass custom data to the notification. Default => []
$payload = [
'myCustomField' => 'This is the content of my customField',
'anotherCustomField' => 'More custom content'
];
// [OPTIONAL] And define, if you need it, a silent notification. Default => false
$silent = true;
// [OPTIONAL] Or/and even a scheduled notification for an indicated datetime. Default => ''
$scheduled = '2016-12-10 10:30:10';
// [OPTIONAL] Filename of audio file to play when a notification is received. Setting this to default will use the default device notification sound. Default => 'default'
$sound = 'default';
// Configure notification:
$ionicPushApi->notifications->setConfig($notificationConfig, $payload, $silent, $scheduled, $sound);
// Send notification to all registered devices:
$ionicPushApi->notifications->sendNotificationToAll();
// or send notification to some devices:
$ionicPushApi->notifications->sendNotification([$desiredToken1, $desiredToken2, $desiredToken3]);
You can read more about this library here: https://github.com/tomloprod/ionic-push-php
Upvotes: 2
Reputation: 1729
I've never dealt with ionic but according to the python example given in http://docs.ionic.io/v1.0/docs/push-sending-push you should add Authorization header to the request.
private_key = YOUR_PRIVATE_API_KEY
b64 = base64.encodestring('%s:' % private_key).replace('\n', '')
#I didn't get why they used colon while encoding the api key??
req.add_header("Authorization", "Basic %s" % b64)
PHP implementation of that python snippet would be like that;
<?php
$yourApiSecret = "YOUR API SECRET";
$androidAppId = "e2c77770";
$data = array(
"tokens" => "APA91bF2YePDKxE6K6vZYs2KQ27Z4mdehJg-EaZaPy10w-RHN5RUgC_P6Uie24Qu_M28j9bfZcbU6pu8Awofa8h2G5j9jABnebrVIUgKM5JcZPEJHYVW2NINirAm7VnSqGOrqm4YicAoI9Xiw5zkgTx4edqXIANLEhvqsqSCeq-_gAuzZB8wvrQ",
"notification" => "Hello World!"
);
$data_string = json_encode($data);
$ch = curl_init('https://push.ionic.io/api/v1/push');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Ionic-Application-Id: '.$androidAppId,
'Content-Length: ' . strlen($data_string),
'Authorization: Basic '.base64_encode($yourApiSecret)
)
);
$result = curl_exec($ch);
var_dump($result);
Also output of the result may tell you a lot about the status of push notification you want to send.
Upvotes: 4