Skip to content

Instantly share code, notes, and snippets.

@nissicreative
Created December 11, 2015 20:05
Show Gist options
  • Save nissicreative/afc174a9457d3b2d69fb to your computer and use it in GitHub Desktop.
Save nissicreative/afc174a9457d3b2d69fb to your computer and use it in GitHub Desktop.
Class provided by Guarang
<?php
//////////////////////////////////////////////////////////
// This Class will be use to send push notification to iPhone device.
//////////////////////////////////////////////////////////
class IOSPushNotify
{
private $apnPath;
private $authId;
function __construct($apnPath = '')
{
if (!empty($apnPath)) {
$this->apnPath = $apnPath;
} else {
$this->apnPath = IOS_PEM_FILE;
}
}
/**
* send_notification function.
*
* @access public
* @param mixed $deviceToken
* @param mixed $message
* @param string $title (default: '')
* @param array $custmParam (default: array())
* @return void
*/
public function send_notification($deviceToken, $message, $title = '', $custmParam = array())
{
$passphrase = '12345';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->apnPath);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$sandbox = IOS_SANDBOX ? 'sandbox.' : '';
$url = "ssl://gateway.{$sandbox}push.apple.com:2195";
$fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
//echo $message="{\"aps\": {\"badge\":1,\"sound\":\"default\",\"alert\": \" ".$message.".\",\"title\": \" ".$title.".\"}, \"verse_id\": 0}",'<br>';
$msgArr = array(
'aps' => array(
'badge' => 1,
'sound' => 'default',
'alert' => $message,
'title' => $title
),
'custom_data' => $custmParam
);
$message = json_encode($msgArr);
$msg = chr(0) . pack("n", 32) . pack('H*', $deviceToken) . pack("n", strlen($message)) . $message;
$fwrite = fwrite($fp, $msg);
//echo '<pre>';print_r($fwrite);die;
fclose($fp);
}
}
@nissicreative
Copy link
Author

$msgArray['apps']['badge'] was originally $msgArray['apps']['bedge']. I thought this to be a typo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment