function addStoryData() {
header("Content-Type:application/json");
$notification = $this->input->post('message');
$device_id = $this->db->select('device_id')
->from('tbl_device')
->where('device_type','iPhone')
->get()
->result_array();
//$this->sendApplePushNotification($device_id,$notification,1);
foreach ($device_id as $d_id) {
$device_id = $d_id['device_id'];
$this->sendApplePushNotification($device_id,$notification,1);
}
}
function sendApplePushNotification($tToken,$tAlert,$notification)
{
/*
print_r($tToken);
echo '----------------------------';
print_r($tAlert);
echo '----------------------------';
print_r($detail);
echo '----------------------------';
print_r($notification);
echo '----------------------------';
print_r($tToken);
die;
*/
$tPayload="test";
//$tHost = 'gateway.sandbox.push.apple.com';
$tHost = 'gateway.push.apple.com';
$tPort = 2195;
// Provide the Certificate and Key Data.
// home/ibliocrq/public_html/shopostreet.com/shopostreet/application/ckShopostreet_development.pem
//echo $this->config->item('pushcertDevelopment.pem');die;
//echo base_url('RestAPI/pushcertDevelopment.pem');
//die;
$tCert = '/home/chatstor/public_html/readify/pushcertDistribution.pem';
//$tCert = '/home/chatstor/public_html/readify/pushcertDevelopment.pem';
//$tCert = 'ckUnionMallProdution.pem';
// Provide the Private Key Passphrase (alternatively you can keep this secrete
// and enter the key manually on the terminal -> remove relevant line from code).
// Replace XXXXX with your Passphrase
$tPassphrase = 'ibl@2017';
// Provide the Device Identifier (Ensure that the Identifier does not have spaces in it).
// Replace this token with the token of the iOS device that is to receive the notification.
//====================
// $tToken = 'da38439e7b2bf83bfae2011eb680debdfeeded66602f685033fd4b25101d31a2';
//====================
// The message that is to appear on the dialog.
//====================
// $tAlert = 'Test Heartfelt Push Notification';
//====================
// The Badge Number for the Application Icon (integer >=0).
// $tBadge = 1;
$tBadge = intval($notification);
// Audible Notification Option.
$tSound = 'default';
// The content that is returned by the LiveCode "pushNotificationReceived" message.
//====================
// $tPayload = 'You get a New Bubble';
//====================
// Create the message content that is to be sent to the device.
$tBody['aps'] = array (
'alert' => $tAlert,
'badge' => $tBadge,
'sound' => $tSound,
//'detail' => $detail,
);
//print_r($tBody);
$tBody ['payload'] = $tPayload;
//$tBody ['bubble_id'] = "101";
// Encode the body to JSON.
$tBody = json_encode ($tBody);
// Create the Socket Stream.
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
// Remove this line if you would like to enter the Private Key Passphrase manually.
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
// Open the Connection to the APNS Server.
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
// Check if we were able to open a socket.
if (!$tSocket)
{
echo json_encode(array("ResponseCode"=>"2","Result"=>"false","ResponseMsg"=> "APNS Connection Failed."));
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
}
// Build the Binary Notification.
/*foreach($tToken as $deviceToken){
echo $tToken['device_id'];
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken['device_id']) . pack ('n', strlen ($tBody)) . $tBody;
// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
if ($tResult){
echo json_encode(array("ResponseCode"=>"1","Result"=>"true","ResponseMsg"=> "Notification has been sent."));
}else{
echo json_encode(array("ResponseCode"=>"2","Result"=>"false","ResponseMsg"=> "Notification could not sent."));
}
}
fclose ($tSocket);*/
//fclose($fp);
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
// Send the Notification to the Server.
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
if ($tResult){
echo json_encode(array("ResponseCode"=>"1","Result"=>"true","ResponseMsg"=> "Notification has been sent."));
}else{
echo json_encode(array("ResponseCode"=>"2","Result"=>"false","ResponseMsg"=> "Notification could not sent."));
}
// Close the Connection to the Server.
fclose ($tSocket);
}
}