Skip to content

Commit

Permalink
Try APNs implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Sep 23, 2014
1 parent d6ed8b9 commit a7f1e8d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
63 changes: 63 additions & 0 deletions src/AuthBucket/Push/ServiceType/ApnsServiceTypeHandler.php
Expand Up @@ -78,5 +78,68 @@ public function unregister(Request $request)

public function send(Request $request)
{
$clientId = $this->checkClientId();

$username = $this->checkUsername();

$data = $this->checkData($request);

$serviceManager = $this->modelManagerFactory->getModelManager('service');
$service = $serviceManager->readModelOneBy(array(
'serviceType' => 'apns',
'clientId' => $clientId,
));
$options = $service->getOptions();

$deviceManager = $this->modelManagerFactory->getModelManager('device');
$devices = $deviceManager->readModelBy(array(
'serviceType' => 'apns',
'clientId' => $clientId,
'username' => $username,
));

// PHP SSL implementation need local_cert as physical file.
// @see http://stackoverflow.com/a/11403788
$local_cert = tempnam(sys_get_temp_dir(), 'PEM');
register_shutdown_function('unlink', $local_cert);
$handler = fopen($local_cert, 'w');
fwrite($handler, $options['local_cert']);
fclose($handler);

$response = array();
foreach ($devices as $device) {
// Prepare the payload in JSON format.
$payload = json_encode(array(
'aps' => array(
'alert' => $data,
'badge' => 1,
'sound' => 'default',
),
));

// Build the message.
$message = chr(0).chr(0);
$message .= chr(32).pack('H*', $device->getDeviceToken());
$message .= chr(strlen($payload)).$payload;

// Create and write to the stream.
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'local_cert', $local_cert);
stream_context_set_option($context, 'ssl', 'passphrase', $options['passphrase']);
$handler = stream_socket_client(
$options['host'],
$error,
$errorString,
3,
STREAM_CLIENT_CONNECT,
$context
);
fwrite($handler, $message);
fclose($handler);

$response[] = $errorString;
}

return $response;
}
}
Expand Up @@ -20,7 +20,7 @@ class DeviceFixture implements FixtureInterface
public function load(ObjectManager $manager)
{
$model = new Device();
$model->setDeviceToken('eeb5aa92bbb4b56373b9e0d00bc02d93')
$model->setDeviceToken('569b74819e2e16f52c9f00d293aefcf78ffa45942dbbdbdc3dcd31369b485a2f')
->setServiceType('apns')
->setClientId('http://democlient1.com/')
->setUsername('demousername1')
Expand Down
Expand Up @@ -23,9 +23,8 @@ public function load(ObjectManager $manager)
$model->setServiceType('apns')
->setClientId('http://democlient1.com/')
->setOptions(array(
'host' => 'gateway.sandbox.push.apple.com',
'port' => '2195',
'pem' => <<<EOF
'host' => 'ssl://gateway.sandbox.push.apple.com:2195',
'local_cert' => <<<EOF
-----BEGIN CERTIFICATE-----
MIIFmzCCBIOgAwIBAgIIEK8htRc6Pt4wDQYJKoZIhvcNAQEFBQAwgZYxCzAJBgNV
BAYTAlVTMRMwEQYDVQQKDApBcHBsZSBJbmMuMSwwKgYDVQQLDCNBcHBsZSBXb3Js
Expand Down Expand Up @@ -94,7 +93,7 @@ public function load(ObjectManager $manager)
-----END RSA PRIVATE KEY-----
EOF
,
'pass' => 'hello123',
'passphrase' => 'hello123',
));
$manager->persist($model);

Expand Down

0 comments on commit a7f1e8d

Please sign in to comment.