Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Feature/bulk sending #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions Service/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,31 @@ public function __construct()
}

/**
* Sends a message to a device, identified by
* Queues a message for sending to a device, identified by
* the OS and the supplied device token
*
* @param \RMS\PushNotificationsBundle\Message\MessageInterface $message
* @throws \RuntimeException
* @return bool
*/
public function queue(MessageInterface $message)
{
if (!isset($this->handlers[$message->getTargetOS()])) {
throw new \RuntimeException("OS type {$message->getTargetOS()} not supported");
}

$handler = $this->handlers[$message->getTargetOS()];

if (method_exists($handler, "queue")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be nice to create a BulkNotificationInterface instead of doing this ?

return $handler->queue($message);
} else {
// Fall back to sending now if bulk messaging not supported
return $handler->send($message);
}
}

/**
* Sends a message to a device instantly, identified by
* the OS and the supplied device token
*
* @param \RMS\PushNotificationsBundle\Message\MessageInterface $message
Expand All @@ -37,13 +61,33 @@ public function send(MessageInterface $message)
return $this->handlers[$message->getTargetOS()]->send($message);
}

/**
* Flush - send queued messages in all handlers
*
* @return array An array of errors returned by each handler, keyed by the OS.
*/
public function flush()
{
$errors = array();
foreach ($this->handlers as $osType => $handler) {
if (method_exists($handler, "flush")) {
// This is a no-op for platforms that don't support bulk sending
$osErrors = $handler->flush();
if (count($osErrors)) {
$errors[$osType] = $osErrors;
}
}
}
return $errors;
}

/**
* Adds a handler
*
* @param $osType
* @param $service
*/
public function addHandler($osType, $service)
public function addHandler($osType, OS\OSNotificationServiceInterface $service)
{
if (!isset($this->handlers[$osType])) {
$this->handlers[$osType] = $service;
Expand Down
68 changes: 44 additions & 24 deletions Service/OS/AppleNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class AppleNotification implements OSNotificationServiceInterface
*/
protected $lastMessageId;

/**
* Log of errors thrown to us from Apple
*
* @var type array
*/
protected $errors = array();

/**
* JSON_UNESCAPED_UNICODE
*
Expand Down Expand Up @@ -88,59 +95,72 @@ public function setJsonUnescapedUnicode($jsonUnescapedUnicode)
}

/**
* Send a notification message
* Queue a notification message for later sending
*
* @param \RMS\PushNotificationsBundle\Message\MessageInterface|\RMS\PushNotificationsBundle\Service\OS\MessageInterface $message
* @param AppleMessage $message
* @throws \RuntimeException
* @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException
* @return bool
* @throws InvalidMessageTypeException
* @return int Id of the message
*/
public function send(MessageInterface $message)
public function queue(MessageInterface $message)
{
if (!$message instanceof AppleMessage) {
throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by APN", get_class($message)));
}

$apnURL = "ssl://gateway.push.apple.com:2195";
if ($this->useSandbox) {
$apnURL = "ssl://gateway.sandbox.push.apple.com:2195";
}

$messageId = ++$this->lastMessageId;
$this->messages[$messageId] = $this->createPayload($messageId, $message->getDeviceIdentifier(), $message->getMessageBody());
$errors = $this->sendMessages($messageId, $apnURL);

return $messageId;
}

/**
* Send a single notification message
*
* @param AppleMessage $message
* @throws \RuntimeException
* @throws InvalidMessageTypeException
* @return bool True if messages sent successfully
*/
public function send(MessageInterface $message)
{
$messageId = $this->queue($message);
$errors = $this->flush($messageId);

return !$errors;
}

/**
* Send all notification messages starting from the given ID
*
* @param int $firstMessageId
* @param string $apnURL
* @param int $firstMessageId
* @param string $apnURL
* @throws \RuntimeException
* @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException
* @return int
* @throws InvalidMessageTypeException
* @return array Array of errors returned
*/
protected function sendMessages($firstMessageId, $apnURL)
protected function flush($firstMessageId = 0)
{
$errors = array();
$apnURL = "ssl://gateway.push.apple.com:2195";
if ($this->useSandbox) {
$apnURL = "ssl://gateway.sandbox.push.apple.com:2195";
}

// Loop through all messages starting from the given ID
for ($currentMessageId = $firstMessageId; $currentMessageId < count($this->messages); $currentMessageId++)
{
for ($currentMessageId = $firstMessageId; $currentMessageId < count($this->messages); $currentMessageId++) {
// Send the message
$result = $this->writeApnStream($apnURL, $this->messages[$currentMessageId]);

$errors = array();

// Check if there is an error result
if (is_array($result)) {
// Save the error
$this->errors[] = $result;
// Resend all messages that were sent after the failed message
$this->sendMessages($result['identifier']+1, $apnURL);
$errors[] = $result;
$this->flush($result['identifier']+1, $apnURL);
}
}
return $errors;

return $this->$errors;
}

/**
Expand Down