Skip to content

Instantly share code, notes, and snippets.

@uoxiu
Created March 21, 2016 09:10
Show Gist options
  • Save uoxiu/0a4b3961d58c76b7514c to your computer and use it in GitHub Desktop.
Save uoxiu/0a4b3961d58c76b7514c to your computer and use it in GitHub Desktop.
<?php
protected function sendPushNotificationsToIos($message) {
$message = "test mesaj ".date("Y-m-d H:i:s");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'] . '/key/ebsNotifKey.pem');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
$err,
$errstr,
60,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$ctx);
if ($fp) {
$body['aps'] = array(
'badge' => +1,
'alert' => $message,
'sound' => 'default'
);
$payload = json_encode($body);
$list = array();
$codes = $this->db->from('ci_appcode')->where('enabled', 1)->where('type', 'ios')->get()->result_array();
foreach ($codes as $code) {
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $code['code'])) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
$list[] = 'Message not delivered to ' . $code['code'] . PHP_EOL;
else
$list[] = 'Message successfully delivered to ' . $code['code'] . ' message: ' .$message. PHP_EOL;
}
return $list;
fclose($fp);
} else {
return array('Cannot connect to server' . PHP_EOL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment