Skip to content

Instantly share code, notes, and snippets.

@fotiDim
Last active December 11, 2015 07:18
Show Gist options
  • Save fotiDim/656d3c9236d5bcbf3864 to your computer and use it in GitHub Desktop.
Save fotiDim/656d3c9236d5bcbf3864 to your computer and use it in GitHub Desktop.
GIST of a Joomla plugin that tries to send APNS push message whenever someone publishes an article
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
define( 'YOURBASEPATH', dirname(__FILE__) );
require_once YOURBASEPATH . '/push.php';
class plgSystemContentpush extends JPlugin
{
public function onContentAfterSave($context, $article, $isNew)
{
$app = JFactory::getApplication();
if ($isNew == false)
{
JFactory::getApplication()->enqueueMessage('ArticleID: '.$article->id.' Title: '.$article->title);
JFactory::getApplication()->enqueueMessage(pushAPNS::sendpush($article->id,$article->title));
}
}
}
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class pushAPNS
{
public function sendpush($id,$message)
{
JFactory::getApplication()->enqueueMessage('called');
$serverId = "0b9214ea";
$name = "APNS";
$apnsPort = 2195;
$passPhrase = "thanasis";
$fwrite = "";
$sslUrl = "ssl://gateway.sandbox.push.apple.com:" . $apnsPort;
$apnsCert = "apns-dev.pem";//give the apns.pem file path on your server
$badge = 1;
//$message = 'My first Push notification';
//$apnspayload['aps'] = array ('alert' => $message,'badge' => $badge,'sound' => 'default');
$apnspayload['aps'] = array ('alert' => $message,'id' => $id);
$payload = json_encode($apnspayload);
$tokenId = 'd67324cb e44bb5a6 88ceb1ba 1cb2fbf4 17f130ee dd56782d 450003c1 9ea4f5b1'; //add your device token ID here
$apnsMessage = chr(1) . pack('N', time()) . pack('N', time() + 86400) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $tokenId)) . chr(0) . chr(strlen($payload)) . $payload;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $passPhrase);
try
{
$apns = stream_socket_client($sslUrl, $error, $errorString, 6, STREAM_CLIENT_CONNECT, $streamContext);
}
catch (Exception $e)
{
// An exception has been caught, just echo the message.
fwrite(STDOUT, $e->getMessage() . "\n");
exit($e->getCode());
}
if($apns)
{
$fwrite = fwrite($apns, $apnsMessage);
fclose($apns);
@socket_close($apns);
return 'true';
}
else
{
return 'Error '.$error.': '.$errorString;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment