Skip to content

Commit

Permalink
Version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
baptiste-dulac committed Mar 13, 2018
1 parent f1317cf commit 35c21c1
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 66 deletions.
16 changes: 8 additions & 8 deletions DependencyInjection/BaptisteDulacPushNotificationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
if (isset($config['android'])) {
$this->container->setParameter('bd_push_notifications.android.enabled', true);
$this->container->setParameter("bd_push_notifications.android.api_key", $config["android"]["timeout"]);
$this->container->setParameter("bd_push_notifications.android.api_key", $config["android"]["api_key"]);
$this->container->setParameter('baptiste_dulac_push_notification.android.enabled', true);
$this->container->setParameter("baptiste_dulac_push_notification.android.timeout", $config["android"]["timeout"]);
$this->container->setParameter("baptiste_dulac_push_notification.android.api_key", $config["android"]["api_key"]);
$loader->load('android.yaml');
}

Expand All @@ -48,10 +48,10 @@ private function addIOSConfig(array $config): void
throw new \RuntimeException(sprintf('Pem file "%s" not found.', $config["pem"]));
}

$this->container->setParameter('bd_push_notifications.ios.enabled', true);
$this->container->setParameter('bd_push_notifications.ios.timeout', $config["timeout"]);
$this->container->setParameter('bd_push_notifications.ios.sandbox', $config["sandbox"]);
$this->container->setParameter('bd_push_notifications.ios.pem', $pemFile);
$this->container->setParameter('bd_push_notifications.ios.passphrase', $config["passphrase"]);
$this->container->setParameter('baptiste_dulac_push_notification.ios.enabled', true);
$this->container->setParameter('baptiste_dulac_push_notification.ios.timeout', $config["timeout"]);
$this->container->setParameter('baptiste_dulac_push_notification.ios.sandbox', $config["sandbox"]);
$this->container->setParameter('baptiste_dulac_push_notification.ios.pem', $pemFile);
$this->container->setParameter('baptiste_dulac_push_notification.ios.passphrase', $config["passphrase"]);
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder
->root("baptiste_dulac_push_notifications")->
->root("baptiste_dulac_push_notification")->
children()->
arrayNode("android")->
canBeUnset()->
Expand Down
50 changes: 50 additions & 0 deletions Message/AbstractMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace BaptisteDulac\PushNotificationBundle\Message;

use BaptisteDulac\PushNotificationBundle\MessageInterface;

abstract class AbstractMessage implements MessageInterface
{

private $id;

private $title;

private $message;

private $devices = [];

public function __construct(string $id, string $title, string $message)
{
$this->id = $id;
$this->title = $title;
$this->message = $message;
}

public function addDevice(string $id)
{
$this->devices[] = $id;
}

public function id(): string
{
return $this->id;
}

public function title(): string
{
return $this->title;
}

public function message(): string
{
return $this->message;
}

public function devices(): array
{
return $this->devices;
}

}
21 changes: 1 addition & 20 deletions Message/AndroidMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,8 @@

use BaptisteDulac\PushNotificationBundle\MessageInterface;

class AndroidMessage implements MessageInterface
class AndroidMessage extends AbstractMessage implements MessageInterface
{

private $title;

private $message;

public function __construct(string $title, string $message)
{
$this->title = $title;
$this->message = $message;
}

public function title(): string
{
return $this->title;
}

public function message(): string
{
return $this->message;
}

}
21 changes: 1 addition & 20 deletions Message/IOSMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@

use BaptisteDulac\PushNotificationBundle\MessageInterface;

class IOSMessage implements MessageInterface
class IOSMessage extends AbstractMessage implements MessageInterface
{

private $title;

private $message;

public function __construct(string $title, string $message)
{
$this->title = $title;
$this->message = $message;
}

public function title(): string
{
return $this->title;
}

public function message(): string
{
return $this->message;
}
}
6 changes: 5 additions & 1 deletion MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
interface MessageInterface
{

public function __construct(string $title, string $message);
public function __construct(string $id, string $title, string $message);

public function id(): string;

public function title(): string;

public function message(): string;

public function devices(): array;

}
17 changes: 14 additions & 3 deletions PushNotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BaptisteDulac\PushNotificationBundle;

use BaptisteDulac\PushNotificationBundle\Message\AndroidMessage;
use BaptisteDulac\PushNotificationBundle\Message\IOSMessage;
use BaptisteDulac\PushNotificationBundle\Sender\AndroidSender;
use BaptisteDulac\PushNotificationBundle\Sender\IOSSender;

Expand All @@ -12,20 +14,29 @@ class PushNotificationSender

private $iOS;

public function __construct(AndroidSender $androidSender = null, IOSSender $iOSSender = null)
private $messages = [];

public function __construct(AndroidSender $androidSender, IOSSender $iOSSender)
{
$this->android = $androidSender;
$this->iOS = $iOSSender;
}

public function addMessage(MessageInterface $message)
{

$this->messages[] = $message;
}

public function send()
{

foreach ($this->messages as $message)
{
if ($message instanceof IOSMessage && $this->iOS != null) {
$this->iOS->send($message);
} else if ($message instanceof AndroidMessage && $this->android != null) {
$this->android->send($message);
}
}
}

}
6 changes: 3 additions & 3 deletions Resources/config/android.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
BaptisteDulac\Sender\AndroidSender:
BaptisteDulac\PushNotificationBundle\Sender\AndroidSender:
arguments:
- '%bd_push_notification.android.api_key%'
- '%bd_push_notification.android.timeout%'
- '%baptiste_dulac_push_notification.android.api_key%'
- '%baptiste_dulac_push_notification.android.timeout%'
10 changes: 5 additions & 5 deletions Resources/config/ios.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
BaptisteDulac\Sender\IOSSender:
BaptisteDulac\PushNotificationBundle\Sender\IOSSender:
arguments:
- '%bd_push_notification.ios.pem%'
- '%bd_push_notification.ios.passphrase%'
- '%bd_push_notification.ios.sandbox%'
- '%bd_push_notification.ios.timeout%'
- '%baptiste_dulac_push_notification.ios.pem%'
- '%baptiste_dulac_push_notification.ios.passphrase%'
- '%baptiste_dulac_push_notification.ios.sandbox%'
- '%baptiste_dulac_push_notification.ios.timeout%'
6 changes: 6 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
BaptisteDulac\PushNotificationBundle\PushNotificationSender:
public: false
arguments:
- '@BaptisteDulac\PushNotificationBundle\Sender\AndroidSender'
- '@BaptisteDulac\PushNotificationBundle\Sender\IOSSender'
38 changes: 36 additions & 2 deletions Sender/AndroidSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,47 @@

namespace BaptisteDulac\PushNotificationBundle\Sender;

use BaptisteDulac\PushNotificationBundle\MessageInterface;
use BaptisteDulac\PushNotificationBundle\SenderInterface;

class AndroidSender implements SenderInterface
{

public function send(array $messages, array $devicesIdentifier)
private $apiKey;

private $timeout;

public function __construct(string $apiKey, string $timeout)
{
$this->apiKey = $apiKey;
$this->timeout = $timeout;
}


public function send(MessageInterface $message): void
{
// TODO: Implement send() method.
$headers = [
'Authorization: key=' . $this->apiKey,
'Content-Type: application/json'
];

$data = [
'data' => [
"title" => $message->title(),
"message" => $message->message(),
"notId" => $message->id()
],
'registration_ids' => $message->devices(),
];

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $data ) );
$result = curl_exec($ch );
curl_close( $ch );
}
}
70 changes: 68 additions & 2 deletions Sender/IOSSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,79 @@

namespace BaptisteDulac\PushNotificationBundle\Sender;

use BaptisteDulac\PushNotificationBundle\MessageInterface;
use BaptisteDulac\PushNotificationBundle\SenderInterface;

class IOSSender implements SenderInterface
{
private $pem;

public function send(array $messages, array $devicesIdentifier)
private $passPhrase;

private $sandbox;

private $timeout;

const SANDBOX_ENDPOINT = "ssl://gateway.sandbox.push.apple.com:2195";

//const ENDPOINT = "ssl://gateway.push.apple.com:2195";

public function __construct(string $pem, string $passPhrase, bool $sandbox = false, $timeout = 60)
{
$this->pem = $pem;
$this->passPhrase = $passPhrase;
$this->sandbox = $sandbox;
$this->timeout = $timeout;
}

private function buildPlayload(MessageInterface $message): string
{
return json_encode([
'aps' => [
'alert' => [
'title' => $message->title(),
'body' => $message->message(),
],
'badge' => 0,
'sound' => 'default',
]
]);
}

private function buildRequest(string $playload, string $device)
{
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->pem);
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passPhrase);
$fp = stream_socket_client(
self::SANDBOX_ENDPOINT,
$err,
$errstr,
$this->timeout,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$ctx
);

if (!$fp)
throw new \RuntimeException("Failed to connect: $err $errstr" . PHP_EOL);

$data = chr(0)
.pack('n', 32)
.pack('H*', $device)
.pack('n', strlen($playload))
.$playload;
$result = fwrite($fp, $data, strlen($data));

if (!$result)
throw new \RuntimeException("Message was not delivered");

fclose($fp);
}

public function send(MessageInterface $message): void
{
// TODO: Implement send() method.
$playload = $this->buildPlayload($message);
foreach ($message->devices() as $device)
$this->buildRequest($playload, $device);
}
}
2 changes: 1 addition & 1 deletion SenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
interface SenderInterface
{

public function send(array $messages, array $devicesIdentifier);
public function send(MessageInterface $message): void;

}

0 comments on commit 35c21c1

Please sign in to comment.