diff --git a/Command/TestPushCommand.php b/Command/TestPushCommand.php index df87e40..fab01a8 100644 --- a/Command/TestPushCommand.php +++ b/Command/TestPushCommand.php @@ -8,8 +8,8 @@ Symfony\Component\Console\Input\InputInterface, Symfony\Component\Console\Input\InputOption, Symfony\Component\Console\Output\OutputInterface; -use RMS\PushNotificationsBundle\Message as PushMessage, - RMS\PushNotificationsBundle\Message\MessageInterface; +use RMS\PushNotifications\Message as PushMessage, + RMS\PushNotifications\Message\MessageInterface; class TestPushCommand extends ContainerAwareCommand { @@ -30,7 +30,7 @@ protected function configure() ->setDescription("Sends a push command to a supplied push token'd device") ->addOption("badge", "b", InputOption::VALUE_OPTIONAL, "Badge number (for iOS devices)", 0) ->addOption("text", "t", InputOption::VALUE_OPTIONAL, "Text message") - ->addArgument("service", InputArgument::REQUIRED, "One of 'ios', 'c2dm', 'gcm', 'mac', 'blackberry' or 'windowsphone'") + ->addArgument("service", InputArgument::REQUIRED, "One of 'ios', 'c2dm', 'gcm', 'fcm', 'mac', 'blackberry' or 'windowsphone'") ->addArgument("token", InputArgument::REQUIRED, "Authentication token for the service") ->addArgument("payload", InputArgument::OPTIONAL, "The payload data to send (JSON)", '{"data": "test"}') ; @@ -111,6 +111,11 @@ protected function getMessageClass($service) $message = new PushMessage\AndroidMessage(); $message->setGCM(true); + return $message; + case "fcm": + $message = new PushMessage\AndroidMessage(); + $message->setFCM(true); + return $message; case "blackberry": return new PushMessage\BlackberryMessage(); diff --git a/DependencyInjection/Compiler/AddHandlerPass.php b/DependencyInjection/Compiler/AddHandlerPass.php index 3151328..97ec303 100644 --- a/DependencyInjection/Compiler/AddHandlerPass.php +++ b/DependencyInjection/Compiler/AddHandlerPass.php @@ -4,9 +4,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface, Symfony\Component\DependencyInjection\ContainerBuilder, - Symfony\Component\DependencyInjection\Definition, Symfony\Component\DependencyInjection\Reference, - RMS\PushNotificationsBundle\Device\Types, Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; class AddHandlerPass implements CompilerPassInterface @@ -53,7 +51,7 @@ public function process(ContainerBuilder $container) } // Required interface - $requiredInterface = 'RMS\\PushNotificationsBundle\\Service\\OS\\OSNotificationServiceInterface'; + $requiredInterface = 'RMS\\PushNotifications\\Handlers\\NotificationHandlerInterface'; if (!$refClass->implementsInterface($requiredInterface)) { throw new \UnexpectedValueException(sprintf( 'Notification service "%s" by id "%s" must be implements "%s" interface!' , diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9d8f30f..b098ef5 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,6 +27,7 @@ public function getConfigTreeBuilder() $this->addMac(); $this->addBlackberry(); $this->addWindowsphone(); + $this->addWindows(); return $treeBuilder; } @@ -68,6 +69,13 @@ protected function addAndroid() booleanNode("dry_run")->defaultFalse()->end()-> end()-> end()-> + arrayNode("fcm")-> + canBeUnset()-> + children()-> + scalarNode("api_key")->isRequired()->cannotBeEmpty()->end()-> + booleanNode("use_multi_curl")->defaultValue(true)->end()-> + end()-> + end()-> end()-> end()-> end() @@ -148,4 +156,22 @@ protected function addWindowsphone() end() ; } + + /** + * Windows configuration + */ + protected function addWindows() + { + $this->root-> + children()-> + arrayNode('windows')-> + children()-> + scalarNode("timeout")->defaultValue(5)->end()-> + scalarNode("sid")->isRequired()->cannotBeEmpty()->end()-> + scalarNode("secret")->isRequired()->cannotBeEmpty()->end()-> + end()-> + end()-> + end() + ; + } } diff --git a/DependencyInjection/RMSPushNotificationsExtension.php b/DependencyInjection/RMSPushNotificationsExtension.php index a0c4745..b91e01f 100644 --- a/DependencyInjection/RMSPushNotificationsExtension.php +++ b/DependencyInjection/RMSPushNotificationsExtension.php @@ -58,6 +58,10 @@ public function load(array $configs, ContainerBuilder $container) $this->setWindowsphoneConfig($config); $loader->load('windowsphone.xml'); } + if (isset($config['windows'])) { + $this->setWindowsConfig($config); + $loader->load('windows.xml'); + } } /** @@ -95,13 +99,27 @@ protected function setAndroidConfig(array $config) $this->container->setParameter("rms_push_notifications.android.c2dm.password", $password); $this->container->setParameter("rms_push_notifications.android.c2dm.source", $source); + // DEFINE PARAMETERS + $this->container->setParameter("rms_push_notifications.android.gcm.api_key", null); + $this->container->setParameter("rms_push_notifications.android.gcm.use_multi_curl", null); + $this->container->setParameter("rms_push_notifications.android.gcm.dry_run", null); + $this->container->setParameter("rms_push_notifications.android.fcm.api_key", null); + $this->container->setParameter("rms_push_notifications.android.fcm.use_multi_curl", null); + // GCM $this->container->setParameter("rms_push_notifications.android.gcm.enabled", isset($config["android"]["gcm"])); if (isset($config["android"]["gcm"])) { - $this->container->setParameter("rms_push_notifications.android.gcm.api_key", $config["android"]["gcm"]["api_key"]); + $this->container->setParameter("rms_push_notifications.android.gcm.api_key", isset($config["android"]["gcm"]["api_key"]) ? $config["android"]["gcm"]["api_key"] : null); $this->container->setParameter("rms_push_notifications.android.gcm.use_multi_curl", $config["android"]["gcm"]["use_multi_curl"]); $this->container->setParameter('rms_push_notifications.android.gcm.dry_run', $config["android"]["gcm"]["dry_run"]); } + + // FCM + $this->container->setParameter("rms_push_notifications.android.fcm.enabled", isset($config["android"]["fcm"])); + if (isset($config["android"]["fcm"])) { + $this->container->setParameter("rms_push_notifications.android.fcm.api_key", $config["android"]["fcm"]["api_key"]); + $this->container->setParameter("rms_push_notifications.android.fcm.use_multi_curl", $config["android"]["fcm"]["use_multi_curl"]); + } } /** @@ -192,4 +210,12 @@ protected function setWindowsphoneConfig(array $config) $this->container->setParameter("rms_push_notifications.windowsphone.enabled", true); $this->container->setParameter("rms_push_notifications.windowsphone.timeout", $config["windowsphone"]["timeout"]); } + + protected function setWindowsConfig(array $config) + { + $this->container->setParameter("rms_push_notifications.windows.enabled", true); + $this->container->setParameter("rms_push_notifications.windows.timeout", $config["windows"]["timeout"]); + $this->container->setParameter("rms_push_notifications.windows.sid", $config["windows"]["sid"]); + $this->container->setParameter("rms_push_notifications.windows.secret", $config["windows"]["secret"]); + } } diff --git a/Device/Types.php b/Device/Types.php deleted file mode 100644 index c97e408..0000000 --- a/Device/Types.php +++ /dev/null @@ -1,14 +0,0 @@ -timestamp = $token["timestamp"]; - $this->tokenLength = $token["length"]; - $this->uuid = $token["token"]; - - return $this; - } -} diff --git a/Exception/InvalidMessageTypeException.php b/Exception/InvalidMessageTypeException.php deleted file mode 100644 index 91c4645..0000000 --- a/Exception/InvalidMessageTypeException.php +++ /dev/null @@ -1,7 +0,0 @@ -message = $message; - } - - /** - * Returns the string message - * - * @return string - */ - public function getMessage() - { - return $this->message; - } - - /** - * Sets the data. For Android, this is any custom data to use - * - * @param array $data The custom data to send - */ - public function setData($data) - { - $this->data = (is_array($data) ? $data : array($data)); - } - - /** - * Returns any custom data - * - * @return array - */ - public function getData() - { - return array_merge(array('message' => $this->getMessage()), $this->data); - } - - /** - * Gets the message body to send - * This is primarily used in C2DM - * - * @return array - */ - public function getMessageBody() - { - $data = array( - "registration_id" => $this->identifier, - "collapse_key" => $this->collapseKey, - "data.message" => $this->message, - ); - if (!empty($this->data)) { - $data = array_merge($data, $this->data); - } - - return $data; - } - - /** - * Sets the identifier of the target device, eg UUID or similar - * - * @param $identifier - */ - public function setDeviceIdentifier($identifier) - { - $this->identifier = $identifier; - $this->allIdentifiers = array($identifier => $identifier); - } - - /** - * Returns the target OS for this message - * - * @return string - */ - public function getTargetOS() - { - return ($this->isGCM ? Types::OS_ANDROID_GCM : Types::OS_ANDROID_C2DM); - } - - /** - * Returns the target device identifier - * - * @return string - */ - public function getDeviceIdentifier() - { - return $this->identifier; - } - - /** - * Android-specific - * Returns the collapse key - * - * @return int - */ - public function getCollapseKey() - { - return $this->collapseKey; - } - - /** - * Android-specific - * Sets the collapse key - * - * @param $collapseKey - */ - public function setCollapseKey($collapseKey) - { - $this->collapseKey = $collapseKey; - } - - /** - * Set whether this is a GCM message - * (default false) - * - * @param $gcm - */ - public function setGCM($gcm) - { - $this->isGCM = !!$gcm; - } - - /** - * Returns whether this is a GCM message - * - * @return mixed - */ - public function isGCM() - { - return $this->isGCM; - } - - /** - * Returns an array of device identifiers - * Not used in C2DM - * - * @return mixed - */ - public function getGCMIdentifiers() - { - return array_values($this->allIdentifiers); - } - - /** - * Adds a device identifier to the GCM list - * @param string $identifier - */ - public function addGCMIdentifier($identifier) - { - $this->allIdentifiers[$identifier] = $identifier; - } - - /** - * Sets the GCM list - * @param array $allIdentifiers - */ - public function setAllIdentifiers($allIdentifiers) { - $this->allIdentifiers = array_combine($allIdentifiers, $allIdentifiers); - } - - /** - * Sets GCM options - * @param array $options - */ - public function setGCMOptions($options) - { - $this->gcmOptions = $options; - } - - /** - * Returns GCM options - * - * @return array - */ - public function getGCMOptions() - { - return $this->gcmOptions; - } -} diff --git a/Message/AppleMessage.php b/Message/AppleMessage.php deleted file mode 100644 index 9025aa2..0000000 --- a/Message/AppleMessage.php +++ /dev/null @@ -1,289 +0,0 @@ -apsBody = array( - "aps" => array( - ), - ); - - if ($identifier !== NULL) { - $this->identifier = $identifier; - } - } - - /** - * Sets the message. For iOS, this is the APS alert message - * - * @param $message - */ - public function setMessage($message) - { - $this->apsBody["aps"]["alert"] = $message; - } - - /** - * Sets any custom data for the APS body - * - * @param array $data - */ - public function setData($data) - { - if (!is_array($data)) { - throw new \InvalidArgumentException(sprintf('Messages custom data must be array, "%s" given.', gettype($data))); - } - - if (array_key_exists("aps", $data)) { - unset($data["aps"]); - } - - foreach ($data as $key => $value) { - $this->addCustomData($key, $value); - } - - return $this; - } - - /** - * Add custom data - * - * @param string $key - * @param mixed $value - */ - public function addCustomData($key, $value) - { - if ($key == 'aps') { - throw new \LogicException('Can\'t replace "aps" data. Please call to setMessage, if your want replace message text.'); - } - - if (is_object($value)) { - if (interface_exists('JsonSerializable') && !$value instanceof \stdClass && !$value instanceof \JsonSerializable) { - throw new \InvalidArgumentException(sprintf( - 'Object %s::%s must be implements JsonSerializable interface for next serialize data.', - get_class($value), spl_object_hash($value) - )); - } - } - - $this->customData[$key] = $value; - - return $this; - } - - /** - * Sets the identifier of the target device, eg UUID or similar - * - * @param $identifier - */ - public function setDeviceIdentifier($identifier) - { - $this->identifier = $identifier; - } - - /** - * Gets the full message body to send to APN - * - * @return array - */ - public function getMessageBody() - { - $payloadBody = $this->apsBody; - if (!empty($this->customData)) { - $payloadBody = array_merge($payloadBody, $this->customData); - } - - return $payloadBody; - } - - /** - * Returns the device identifier - * - * @return null|string - */ - public function getDeviceIdentifier() - { - return $this->identifier; - } - - /** - * Returns the target OS for this message - * Must be implemented by subclass - * - * @return string - */ - public function getTargetOS() - { - return ""; - } - - /** - * iOS-specific - * Sets the APS sound - * - * @param string $sound The sound to use. Use 'default' to use the built-in default - */ - public function setAPSSound($sound) - { - $this->apsBody["aps"]["sound"] = $sound; - } - - /** - * iOS-specific - * Sets the APS badge count - * - * @param integer $badge The badge count to display - */ - public function setAPSBadge($badge) - { - $this->apsBody["aps"]["badge"] = (int) $badge; - } - - /** - * iOS-specific - * Sets the APS content available flag, used to transform the notification into remote-notification - * and trigger the "didReceiveRemoteNotification: fetchCompletionHandler:" method on iOS apps - * - * @param string $contentAvailable The flag to set the content-available option, for example set it to 1. - */ - public function setAPSContentAvailable($contentAvailable) - { - $this->apsBody["aps"]["content-available"] = $contentAvailable; - } - - /** - * iOS-specific - * Sets the APS category - * - * @param string $category The notification category - */ - public function setCategory($category) - { - $this->apsBody["aps"]["category"] = $category; - } - - /** - * Set expiry of message - * - * @param int $expiry - */ - public function setExpiry($expiry) - { - $this->expiry = $expiry; - } - - /** - * Get expiry of message - * - * @return int - */ - public function getExpiry() - { - return $this->expiry; - } - - /** - * @param string $pushMagicToken - */ - public function setPushMagicToken($pushMagicToken) - { - $this->pushMagicToken = $pushMagicToken; - } - - /** - * @return string - */ - public function getPushMagicToken() - { - return $this->pushMagicToken; - } - - /** - * @return string - */ - public function getToken() - { - return $this->token; - } - - /** - * @param string $token - */ - public function setToken($token) - { - $this->token = $token; - } - - /** - * @param null|bool $isMdmMessage - * - * @return bool|null - */ - public function isMdmMessage($isMdmMessage = null) - { - if ($isMdmMessage === null) { - return $this->isMdmMessage; - } - - $this->isMdmMessage = (bool) $isMdmMessage; - } -} diff --git a/Message/BlackberryMessage.php b/Message/BlackberryMessage.php deleted file mode 100644 index 0000a4c..0000000 --- a/Message/BlackberryMessage.php +++ /dev/null @@ -1,84 +0,0 @@ -setData($message); - } - - /** - * Sets the data. For Blackberry, this is any data required - * - * @param array $data The custom data to send - */ - public function setData($data) - { - $this->data = $data; - } - - /** - * Gets the message body to send - * For Blackberry, this is just our data object - * - * @return array - */ - public function getMessageBody() - { - return $this->data; - } - - /** - * Sets the identifier of the target device, eg UUID or similar - * - * @param $identifier - */ - public function setDeviceIdentifier($identifier) - { - $this->identifier = $identifier; - } - - /** - * Returns the target OS for this message - * - * @return string - */ - public function getTargetOS() - { - return Types::OS_BLACKBERRY; - } - - /** - * Returns the target device identifier - * - * @return string - */ - public function getDeviceIdentifier() - { - return $this->identifier; - } -} diff --git a/Message/MacMessage.php b/Message/MacMessage.php deleted file mode 100644 index 5291ba9..0000000 --- a/Message/MacMessage.php +++ /dev/null @@ -1,18 +0,0 @@ - 2 - ); - - protected $identifier; - - protected $text1 = ''; - - protected $text2 = ''; - - protected $target; - - public function __construct() - { - $this->target = self::TYPE_TOAST; - } - - public function getTargetOS() - { - return Types::OS_WINDOWSPHONE; - } - - public function getDeviceIdentifier() - { - return $this->identifier; - } - - public function setDeviceIdentifier($identifier) - { - $this->identifier = $identifier; - } - - public function getMessageBody() - { - return array( - 'text1' => $this->text1, - 'text2' => $this->text2 - ); - } - - public function setMessage($message) - { - $this->text2 = $message; - } - - public function setData($data) - { - // Not implemented yet - } - - public function getTarget() - { - return $this->target; - } - - public function getNotificationClass() - { - return static::$notificationClass[$this->getTarget()]; - } -} diff --git a/Message/iOSMessage.php b/Message/iOSMessage.php deleted file mode 100644 index 03e3142..0000000 --- a/Message/iOSMessage.php +++ /dev/null @@ -1,18 +0,0 @@ - # This is titled "Server Key" when creating it use_multi_curl: # default is true dry_run: + fcm: + api_key: # This is titled "Server Key" when creating it + use_multi_curl: # default is true ios: timeout: 60 # Seconds to wait for connection timeout, default is 60 sandbox: @@ -61,8 +64,12 @@ only be available if you provide configuration respectively for them. password: windowsphone: timeout: 5 # Seconds to wait for connection timeout, default is 5 + windows: + timeout: 5 # Seconds to wait for connection timeout, default is 5 + sid: # SID (Secure Identifier) as according to your app's entry in the windows dev center + secret: # Client secret according to your app's entry in the windows dev center -NOTE: If you are using Windows, you may need to set the Android GCM `use_multi_curl` flag to false for GCM messages to be sent correctly. +NOTE: If you are using Windows, you may need to set the Android GCM/FCM `use_multi_curl` flag to false for GCM/FCM messages to be sent correctly. Timeout defaults are the defaults from prior to the introduction of this configuration value. @@ -70,7 +77,7 @@ Timeout defaults are the defaults from prior to the introduction of this configu A little example of how to push your first message to an iOS device, we'll assume that you've set up the configuration correctly: - use RMS\PushNotificationsBundle\Message\iOSMessage; + use RMS\PushNotifications\Message\iOSMessage; class PushDemoController extends Controller { @@ -92,12 +99,13 @@ The send method will detect the type of message so if you'll pass it an `Android Since both C2DM and GCM are still available, the `AndroidMessage` class has a small flag on it to toggle which service to send it to. Use as follows: - use RMS\PushNotificationsBundle\Message\AndroidMessage; + use RMS\PushNotifications\Message\AndroidMessage; $message = new AndroidMessage(); $message->setGCM(true); + $message->setFCM(true); // Use to Firebase Cloud Messaging -to send as a GCM message rather than C2DM. +to send as a FCM message rather than GCM or C2DM. ## iOS Feedback service @@ -116,6 +124,11 @@ Apple recommend you poll this service daily. The bundle has beta support for Windows Phone, and supports the Toast notification. Use the `WindowsphoneMessage` message class to send accordingly. +## Windows (Universal, WNS) - Toast support + +The bundle has beta support for Windows Notification Service (WNS), and supports the Toast notification. Use the `WindowsMessage` message class to send accordingly. + + # Thanks Firstly, thanks to all contributors to this bundle! diff --git a/Resources/config/android.xml b/Resources/config/android.xml index 5802e00..50b0a93 100644 --- a/Resources/config/android.xml +++ b/Resources/config/android.xml @@ -4,8 +4,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\OS\AndroidNotification - RMS\PushNotificationsBundle\Service\OS\AndroidGCMNotification + RMS\PushNotifications\Handlers\AndroidNotificationHandler + RMS\PushNotifications\Handlers\AndroidGCMNotificationHandler + RMS\PushNotifications\Handlers\AndroidFCMNotificationHandler @@ -24,12 +25,20 @@ %rms_push_notifications.android.gcm.api_key% %rms_push_notifications.android.gcm.use_multi_curl% %rms_push_notifications.android.timeout% - null %rms_push_notifications.android.gcm.dry_run% + + + %rms_push_notifications.android.fcm.api_key% + %rms_push_notifications.android.fcm.use_multi_curl% + %rms_push_notifications.android.timeout% + null + + + diff --git a/Resources/config/blackberry.xml b/Resources/config/blackberry.xml index eb9b5a0..0bfd0bd 100644 --- a/Resources/config/blackberry.xml +++ b/Resources/config/blackberry.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\OS\BlackberryNotification + RMS\PushNotifications\Handlers\BlackberryNotificationHandler @@ -15,7 +15,6 @@ %rms_push_notifications.blackberry.app_id% %rms_push_notifications.blackberry.password% %rms_push_notifications.blackberry.timeout% - diff --git a/Resources/config/ios.xml b/Resources/config/ios.xml index 3ca7bec..3b2704c 100644 --- a/Resources/config/ios.xml +++ b/Resources/config/ios.xml @@ -4,7 +4,8 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\OS\AppleNotification + RMS\PushNotifications\Handlers\AppleNotificationHandler + RMS\PushNotificationsBundle\Service\iOSFeedback @@ -16,9 +17,6 @@ %rms_push_notifications.ios.passphrase% %rms_push_notifications.ios.json_unescaped_unicode% %rms_push_notifications.ios.timeout% - %kernel.cache_dir% - - diff --git a/Resources/config/mac.xml b/Resources/config/mac.xml index e4e4c0e..aeb2925 100644 --- a/Resources/config/mac.xml +++ b/Resources/config/mac.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\OS\AppleNotification + RMS\PushNotifications\Handlers\OS\AppleNotificationHandler @@ -16,9 +16,6 @@ %rms_push_notifications.mac.passphrase% %rms_push_notifications.mac.json_unescaped_unicode% %rms_push_notifications.mac.timeout% - %kernel.cache_dir% - - diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 6784b04..4cd5779 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,23 +4,12 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\Notifications - RMS\PushNotificationsBundle\Service\OS\AndroidNotification - RMS\PushNotificationsBundle\Service\OS\AppleNotification - RMS\PushNotificationsBundle\Service\iOSFeedback - RMS\PushNotificationsBundle\Service\OS\AppleNotification - RMS\PushNotificationsBundle\Service\EventListener + RMS\PushNotifications\Notifications - - - - - - diff --git a/Resources/config/windows.xml b/Resources/config/windows.xml new file mode 100644 index 0000000..3cf2a5c --- /dev/null +++ b/Resources/config/windows.xml @@ -0,0 +1,22 @@ + + + + + RMS\PushNotifications\Handlers\WindowsNotificationHandler + + + + + + + %rms_push_notifications.windows.sid% + %rms_push_notifications.windows.secret% + %rms_push_notifications.windows.timeout% + + + + + + diff --git a/Resources/config/windowsphone.xml b/Resources/config/windowsphone.xml index 6703a17..262fe64 100644 --- a/Resources/config/windowsphone.xml +++ b/Resources/config/windowsphone.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - RMS\PushNotificationsBundle\Service\OS\MicrosoftNotification + RMS\PushNotifications\Handlers\MicrosoftNotificationHandler @@ -12,7 +12,6 @@ %rms_push_notifications.windowsphone.timeout% - diff --git a/Service/EventListener.php b/Service/EventListener.php deleted file mode 100644 index bfe8ba2..0000000 --- a/Service/EventListener.php +++ /dev/null @@ -1,28 +0,0 @@ -listeners[] = $listener; - } - - /** - * Call onKernelTerminate on every listener - */ - public function onKernelTerminate () { - foreach ($this->listeners as $listener) { - $listener->onKernelTerminate(); - } - } -} diff --git a/Service/EventListenerInterface.php b/Service/EventListenerInterface.php deleted file mode 100644 index 22d107d..0000000 --- a/Service/EventListenerInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -supports($message->getTargetOS())) { - throw new \RuntimeException("OS type {$message->getTargetOS()} not supported"); - } - - return $this->handlers[$message->getTargetOS()]->send($message); - } - - /** - * Adds a handler - * - * @param $osType - * @param $service - */ - public function addHandler($osType, $service) - { - if (!isset($this->handlers[$osType])) { - $this->handlers[$osType] = $service; - } - } - - /** - * Get responses from handler - * - * @param string $osType - * @return array - * @throws \RuntimeException - */ - public function getResponses($osType) - { - if (!isset($this->handlers[$osType])) { - throw new \RuntimeException("OS type {$osType} not supported"); - } - - if (!method_exists($this->handlers[$osType], 'getResponses')) { - throw new \RuntimeException("Handler for OS type {$osType} not supported getResponses() method"); - } - - return $this->handlers[$osType]->getResponses(); - } - - /** - * Check if target OS is supported - * - * @param $targetOS - * - * @return bool - */ - public function supports($targetOS) - { - return isset($this->handlers[$targetOS]); - } - - - /** - * Set Apple Push Notification Service pem as string. - * Service won't use pem file passed by config anymore. - * - * @param $pemContent string - * @param $passphrase - */ - public function setAPNSPemAsString($pemContent, $passphrase) { - if (isset($this->handlers[Types::OS_IOS]) && $this->handlers[Types::OS_IOS] instanceof AppleNotification) { - /** @var AppleNotification $appleNotification */ - $appleNotification = $this->handlers[Types::OS_IOS]; - $appleNotification->setPemAsString($pemContent, $passphrase); - } - } -} diff --git a/Service/OS/AndroidGCMNotification.php b/Service/OS/AndroidGCMNotification.php deleted file mode 100644 index 69a1c1a..0000000 --- a/Service/OS/AndroidGCMNotification.php +++ /dev/null @@ -1,171 +0,0 @@ -useDryRun = $dryRun; - $this->apiKey = $apiKey; - if (!$client) { - $client = ($useMultiCurl ? new MultiCurl() : new Curl()); - } - $client->setTimeout($timeout); - - $this->browser = new Browser($client); - $this->browser->getClient()->setVerifyPeer(false); - $this->logger = $logger; - } - - /** - * Sends the data to the given registration IDs via the GCM server - * - * @param \RMS\PushNotificationsBundle\Message\MessageInterface $message - * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException - * @return bool - */ - public function send(MessageInterface $message) - { - if (!$message instanceof AndroidMessage) { - throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by GCM", get_class($message))); - } - if (!$message->isGCM()) { - throw new InvalidMessageTypeException("Non-GCM messages not supported by the Android GCM sender"); - } - - $headers = array( - "Authorization: key=" . $this->apiKey, - "Content-Type: application/json", - ); - $data = array_merge( - $message->getGCMOptions(), - array("data" => $message->getData()) - ); - - if ($this->useDryRun) { - $data['dry_run'] = true; - } - - // Perform the calls (in parallel) - $this->responses = array(); - $gcmIdentifiers = $message->getGCMIdentifiers(); - - if (count($message->getGCMIdentifiers()) == 1) { - $data['to'] = $gcmIdentifiers[0]; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); - } else { - // Chunk number of registration IDs according to the maximum allowed by GCM - $chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount); - - foreach ($chunks as $registrationIDs) { - $data['registration_ids'] = $registrationIDs; - $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); - } - } - - // If we're using multiple concurrent connections via MultiCurl - // then we should flush all requests - if ($this->browser->getClient() instanceof MultiCurl) { - $this->browser->getClient()->flush(); - } - - // Determine success - foreach ($this->responses as $response) { - $message = json_decode($response->getContent()); - if ($message === null || $message->success == 0 || $message->failure > 0) { - if ($message == null) { - $this->logger->error($response->getContent()); - } else { - foreach ($message->results as $result) { - if (isset($result->error)) { - $this->logger->error($result->error); - } - } - } - return false; - } - } - - return true; - } - - /** - * Returns responses - * - * @return array - */ - public function getResponses() - { - return $this->responses; - } -} diff --git a/Service/OS/AndroidNotification.php b/Service/OS/AndroidNotification.php deleted file mode 100644 index 2e35ea4..0000000 --- a/Service/OS/AndroidNotification.php +++ /dev/null @@ -1,122 +0,0 @@ -username = $username; - $this->password = $password; - $this->source = $source; - $this->timeout = $timeout; - $this->authToken = ""; - } - - /** - * Sends a C2DM message - * This assumes that a valid auth token can be obtained - * - * @param \RMS\PushNotificationsBundle\Message\MessageInterface $message - * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException - * @return bool - */ - public function send(MessageInterface $message) - { - if (!$message instanceof AndroidMessage) { - throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by C2DM", get_class($message))); - } - - if ($this->getAuthToken()) { - $headers[] = "Authorization: GoogleLogin auth=" . $this->authToken; - $data = $message->getMessageBody(); - - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); - $response = $buzz->post("https://android.apis.google.com/c2dm/send", $headers, http_build_query($data)); - - return preg_match("/^id=/", $response->getContent()) > 0; - } - - return false; - } - - /** - * Gets a valid authentication token - * - * @return bool - */ - protected function getAuthToken() - { - $data = array( - "Email" => $this->username, - "Passwd" => $this->password, - "accountType" => "HOSTED_OR_GOOGLE", - "source" => $this->source, - "service" => "ac2dm" - ); - - $buzz = new Browser(); - $buzz->getClient()->setVerifyPeer(false); - $buzz->getClient()->setTimeout($this->timeout); - $response = $buzz->post("https://www.google.com/accounts/ClientLogin", array(), http_build_query($data)); - if ($response->getStatusCode() !== 200) { - return false; - } - - preg_match("/Auth=([a-z0-9_\-]+)/i", $response->getContent(), $matches); - $this->authToken = $matches[1]; - - return true; - } -} diff --git a/Service/OS/AppleNotification.php b/Service/OS/AppleNotification.php deleted file mode 100644 index 3b19ace..0000000 --- a/Service/OS/AppleNotification.php +++ /dev/null @@ -1,445 +0,0 @@ -useSandbox = $sandbox; - $this->pemPath = $pem; - $this->passphrase = $passphrase; - $this->apnStreams = array(); - $this->messages = array(); - $this->lastMessageId = -1; - $this->jsonUnescapedUnicode = $jsonUnescapedUnicode; - $this->timeout = $timeout; - $this->cachedir = $cachedir; - $this->logger = $logger; - - if ($eventListener != null) { - $eventListener->addListener($this); - } - } - - /** - * Set option JSON_UNESCAPED_UNICODE to json encoders - * - * @param boolean $jsonUnescapedUnicode - */ - public function setJsonUnescapedUnicode($jsonUnescapedUnicode) - { - $this->jsonUnescapedUnicode = (bool) $jsonUnescapedUnicode; - - return $this; - } - - /** - * Send a MDM or notification message - * - * @param \RMS\PushNotificationsBundle\Message\MessageInterface|\RMS\PushNotificationsBundle\Service\OS\MessageInterface $message - * @throws \RuntimeException - * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException - * @return bool - */ - public function send(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; - - if ($message->isMdmMessage()) { - if ($message->getToken() == '') { - throw new InvalidMessageTypeException(sprintf("Message type '%s' is a MDM message but 'token' is missing", get_class($message))); - } - - if ($message->getPushMagicToken() == '') { - throw new InvalidMessageTypeException(sprintf("Message type '%s' is a MDM message but 'pushMagicToken' is missing", get_class($message))); - } - - $this->messages[$messageId] = $this->createMdmPayload($message->getToken(), $message->getPushMagicToken()); - } else { - $this->messages[$messageId] = $this->createPayload($messageId, $message->getExpiry(), $message->getDeviceIdentifier(), $message->getMessageBody()); - } - - $errors = $this->sendMessages($messageId, $apnURL); - - return !$errors; - } - - /** - * Send all notification messages starting from the given ID - * - * @param int $firstMessageId - * @param string $apnURL - * @throws \RuntimeException - * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException - * @return int - */ - protected function sendMessages($firstMessageId, $apnURL) - { - $errors = array(); - // Loop through all messages starting from the given ID - $messagesCount = count($this->messages); - for ($currentMessageId = $firstMessageId; $currentMessageId < $messagesCount; $currentMessageId++) { - // Send the message - $result = $this->writeApnStream($apnURL, $this->messages[$currentMessageId]); - - // Check if there is an error result - if (is_array($result)) { - - // Close the apn stream in case of Shutdown status code. - if ($result['status'] === self::APNS_SHUTDOWN_CODE) { - $this->closeApnStream($apnURL); - } - - $this->responses[] = $result; - // Resend all messages that were sent after the failed message - $this->sendMessages($result['identifier']+1, $apnURL); - $errors[] = $result; - if ($this->logger) { - $this->logger->error(json_encode($result)); - } - } else { - $this->responses[] = true; - } - } - - return $errors; - } - - /** - * Write data to the apn stream that is associated with the given apn URL - * - * @param string $apnURL - * @param string $payload - * @throws \RuntimeException - * @return mixed - */ - protected function writeApnStream($apnURL, $payload) - { - // Get the correct Apn stream and send data - $fp = $this->getApnStream($apnURL); - $response = (strlen($payload) === @fwrite($fp, $payload, strlen($payload))); - - // Check if there is responsedata to read - $readStreams = array($fp); - $null = NULL; - $streamsReadyToRead = @stream_select($readStreams, $null, $null, 1, 0); - if ($streamsReadyToRead > 0) { - // Unpack error response data and set as the result - $response = @unpack("Ccommand/Cstatus/Nidentifier", fread($fp, 6)); - $this->closeApnStream($apnURL); - } - - // Will contain true if writing succeeded and no error is returned yet - return $response; - } - - /** - * Get an apn stream associated with the given apn URL, create one if necessary - * - * @param string $apnURL - * @throws \RuntimeException - * @return resource - */ - protected function getApnStream($apnURL) - { - if (!isset($this->apnStreams[$apnURL])) { - // No stream found, setup a new stream - $ctx = $this->getStreamContext(); - $this->apnStreams[$apnURL] = stream_socket_client($apnURL, $err, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $ctx); - if (!$this->apnStreams[$apnURL]) { - throw new \RuntimeException("Couldn't connect to APN server. Error no $err: $errstr"); - } - - // Reduce buffering and blocking - if (function_exists("stream_set_read_buffer")) { - stream_set_read_buffer($this->apnStreams[$apnURL], 6); - } - stream_set_write_buffer($this->apnStreams[$apnURL], 0); - stream_set_blocking($this->apnStreams[$apnURL], 0); - } - - return $this->apnStreams[$apnURL]; - } - - /** - * Close the apn stream associated with the given apn URL - * - * @param string $apnURL - */ - protected function closeApnStream($apnURL) - { - if (isset($this->apnStreams[$apnURL])) { - // Stream found, close the stream - fclose($this->apnStreams[$apnURL]); - unset($this->apnStreams[$apnURL]); - } - } - - /** - * Gets a stream context set up for SSL - * using our PEM file and passphrase - * - * @return resource - */ - protected function getStreamContext() - { - $pem = $this->pemPath; - $passphrase = $this->passphrase; - - // Create cache pem file if needed - if (!empty($this->pemContent)) { - $filename = $this->cachedir . self::APNS_CERTIFICATE_FILE; - - $fs = new Filesystem(); - $fs->mkdir(dirname($filename)); - file_put_contents($filename, $this->pemContent); - - // Now we use this file as pem - $pem = $filename; - $passphrase = $this->pemContentPassphrase; - } - - $ctx = stream_context_create(); - stream_context_set_option($ctx, "ssl", "local_cert", $pem); - if (strlen($passphrase)) { - stream_context_set_option($ctx, "ssl", "passphrase", $passphrase); - } - - return $ctx; - } - - /** - * Creates the full payload for the notification - * - * @param int $messageId - * @param string $expiry - * @param string $token - * @param array $message - * - * @return string - * - * @throws \LogicException - * @throws \InvalidArgumentException - */ - protected function createPayload($messageId, $expiry, $token, $message) - { - if ($this->jsonUnescapedUnicode) { - // Validate PHP version - if (!version_compare(PHP_VERSION, '5.4.0', '>=')) { - throw new \LogicException(sprintf( - 'Can\'t use JSON_UNESCAPED_UNICODE option on PHP %s. Support PHP >= 5.4.0', - PHP_VERSION - )); - } - - // WARNING: - // Set otpion JSON_UNESCAPED_UNICODE is violation - // of RFC 4627 - // Because required validate charsets (Must be UTF-8) - - $encoding = mb_detect_encoding($message['aps']['alert']); - if ($encoding != 'UTF-8' && $encoding != 'ASCII') { - throw new \InvalidArgumentException(sprintf( - 'Message must be UTF-8 encoding, "%s" given.', - mb_detect_encoding($message['aps']['alert']) - )); - } - - $jsonBody = json_encode($message, JSON_UNESCAPED_UNICODE); - } else { - $jsonBody = json_encode($message); - } - - $token = preg_replace("/[^0-9A-Fa-f]/", "", $token); - $payload = chr(1) . pack("N", $messageId) . pack("N", $expiry) . pack("n", 32) . pack("H*", $token) . pack("n", strlen($jsonBody)) . $jsonBody; - - return $payload; - } - - /** - * Creates a MDM payload - * - * @param string $token - * @param string $magicPushToken - * - * @return string - */ - public function createMdmPayload($token, $magicPushToken) - { - $jsonPayload = json_encode(array('mdm' => $magicPushToken)); - - $payload = chr(0) . chr(0) . chr(32) . base64_decode($token) . chr(0) . chr(strlen($jsonPayload)) . $jsonPayload; - - return $payload; - } - - /** - * Returns responses - * - * @return array - */ - public function getResponses() - { - return $this->responses; - } - - /** - * @param $pemContent - * @param $passphrase - */ - public function setPemAsString($pemContent, $passphrase) { - $this->pemContent = $pemContent; - $this->pemContentPassphrase = $passphrase; - } - - /** - * Called on kernel terminate - */ - public function onKernelTerminate() { - - // Remove cache pem file - $fs = new Filesystem(); - $filename = $this->cachedir . self::APNS_CERTIFICATE_FILE; - if ($fs->exists(dirname($filename))) { - $fs->remove(dirname($filename)); - } - - // Close streams - foreach ($this->apnStreams as $stream) { - fclose($stream); - } - - } -} diff --git a/Service/OS/BlackberryNotification.php b/Service/OS/BlackberryNotification.php deleted file mode 100644 index acc4b4c..0000000 --- a/Service/OS/BlackberryNotification.php +++ /dev/null @@ -1,205 +0,0 @@ -evaluation = $evaluation; - $this->appID = $appID; - $this->password = $password; - $this->timeout = $timeout; - $this->logger = $logger; - } - - /** - * Sends a Blackberry Push message - * - * @param \RMS\PushNotificationsBundle\Message\MessageInterface $message - * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException - * @return bool - */ - public function send(MessageInterface $message) - { - if (!$message instanceof BlackberryMessage) { - throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by Blackberry", get_class($message))); - } - - return $this->doSend($message); - } - - /** - * Does the actual sending - * - * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message - * @return bool - */ - protected function doSend(BlackberryMessage $message) - { - $separator = "mPsbVQo0a68eIL3OAxnm"; - $body = $this->constructMessageBody($message, $separator); - $browser = new Browser(new Curl()); - $browser->getClient()->setTimeout($this->timeout); - $listener = new BasicAuthListener($this->appID, $this->password); - $browser->addListener($listener); - - $url = "https://pushapi.na.blackberry.com/mss/PD_pushRequest"; - if ($this->evaluation) { - $url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"; - } - $headers = array(); - $headers[] = "Content-Type: multipart/related; boundary={$separator}; type=application/xml"; - $headers[] = "Accept: text/html, *"; - $headers[] = "Connection: Keep-Alive"; - - $response = $browser->post($url, $headers, $body); - - return $this->parseResponse($response); - } - - /** - * Builds the actual body of the message - * - * @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message - * @param $separator - * @return string - */ - protected function constructMessageBody(BlackberryMessage $message, $separator) - { - $data = ""; - $messageID = microtime(true); - - $data .= "--" . $separator . "\r\n"; - $data .= "Content-Type: application/xml; charset=UTF-8\r\n\r\n"; - $data .= $this->getXMLBody($message, $messageID) . "\r\n"; - $data .= "--" . $separator . "\r\n"; - $data .= "Content-Type: text/plain\r\n"; - $data .= "Push-Message-ID: {$messageID}\r\n\r\n"; - if (is_array($message->getMessageBody())) { - $data .= json_encode($message->getMessageBody()); - } else { - $data .= $message->getMessageBody(); - } - $data .= "\r\n"; - $data .= "--" . $separator . "--\r\n"; - - return $data; - } - - /** - * Handles and parses the response - * Returns a value indicating success/fail - * - * @param \Buzz\Message\Response $response - * @return bool - */ - protected function parseResponse(\Buzz\Message\Response $response) - { - if (null !== $response->getStatusCode() && $response->getStatusCode() != 200) { - return false; - } - $doc = new \DOMDocument(); - $doc->loadXML($response->getContent()); - $elems = $doc->getElementsByTagName("response-result"); - if (!$elems->length) { - $this->logger->error('Response is empty'); - return false; - } - $responseElement = $elems->item(0); - if ($responseElement->getAttribute("code") != "1001") { - $this->logger->error($responseElement->getAttribute("code"). ' : '. $responseElement->getAttribute("desc")); - } - - return ($responseElement->getAttribute("code") == "1001"); - } - - /** - * Create the XML body that accompanies the actual push data - * - * @param $messageID - * @return string - */ - private function getXMLBody(BlackberryMessage $message, $messageID) - { - $deliverBefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes')); - $impl = new \DOMImplementation(); - $dtd = $impl->createDocumentType( - "pap", - "-//WAPFORUM//DTD PAP 2.1//EN", - "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd" - ); - $doc = $impl->createDocument("", "", $dtd); - - // Build it centre-out - $pm = $doc->createElement("push-message"); - $pm->setAttribute("push-id", $messageID); - $pm->setAttribute("deliver-before-timestamp", $deliverBefore); - $pm->setAttribute("source-reference", $this->appID); - - $qos = $doc->createElement("quality-of-service"); - $qos->setAttribute("delivery-method", "unconfirmed"); - $add = $doc->createElement("address"); - $add->setAttribute("address-value", $message->getDeviceIdentifier()); - - $pm->appendChild($add); - $pm->appendChild($qos); - $pap = $doc->createElement("pap"); - $pap->appendChild($pm); - $doc->appendChild($pap); - - return $doc->saveXML(); - } -} diff --git a/Service/OS/MicrosoftNotification.php b/Service/OS/MicrosoftNotification.php deleted file mode 100644 index 204b1f0..0000000 --- a/Service/OS/MicrosoftNotification.php +++ /dev/null @@ -1,70 +0,0 @@ -browser = new Browser(new Curl()); - $this->browser->getClient()->setVerifyPeer(false); - $this->browser->getClient()->setTimeout($timeout); - $this->logger = $logger; - } - - public function send(MessageInterface $message) - { - if (!$message instanceof WindowsphoneMessage) { - throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by MPNS", get_class($message))); - } - - $headers = array( - 'Content-Type: text/xml', - 'X-WindowsPhone-Target: ' . $message->getTarget(), - 'X-NotificationClass: ' . $message->getNotificationClass() - ); - - $xml = new \SimpleXMLElement(''); - - $msgBody = $message->getMessageBody(); - - if ($message->getTarget() == WindowsphoneMessage::TYPE_TOAST) { - $toast = $xml->addChild('wp:Toast'); - $toast->addChild('wp:Text1', htmlspecialchars($msgBody['text1'], ENT_XML1|ENT_QUOTES)); - $toast->addChild('wp:Text2', htmlspecialchars($msgBody['text2'], ENT_XML1|ENT_QUOTES)); - } - - $response = $this->browser->post($message->getDeviceIdentifier(), $headers, $xml->asXML()); - - if (!$response->isSuccessful()) { - $this->logger->error($response->getStatusCode(). ' : '. $response->getReasonPhrase()); - } - - return $response->isSuccessful(); - } -} diff --git a/Service/OS/OSNotificationServiceInterface.php b/Service/OS/OSNotificationServiceInterface.php deleted file mode 100644 index e82b9a8..0000000 --- a/Service/OS/OSNotificationServiceInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -sandbox = $sandbox; - $this->pem = $pem; - $this->passphrase = $passphrase; - $this->timeout = $timeout; - } - - /** - * Gets an array of device UUID unregistration details - * from the APN feedback service - * - * @throws \RuntimeException - * @return array - */ - public function getDeviceUUIDs() - { - if (!strlen($this->pem)) { - throw new \RuntimeException("PEM not provided"); - } - - $feedbackURL = "ssl://feedback.push.apple.com:2196"; - if ($this->sandbox) { - $feedbackURL = "ssl://feedback.sandbox.push.apple.com:2196"; - } - $data = ""; - - $ctx = $this->getStreamContext(); - $fp = stream_socket_client($feedbackURL, $err, $errstr, $this->timeout, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); - if (!$fp) { - throw new \RuntimeException("Couldn't connect to APNS Feedback service. Error no $err: $errstr"); - } - while (!feof($fp)) { - $data .= fread($fp, 4096); - } - fclose($fp); - if (!strlen($data)) { - return array(); - } - - $feedbacks = array(); - $items = str_split($data, 38); - foreach ($items as $item) { - $feedback = new Feedback(); - $feedbacks[] = $feedback->unpack($item); - } - - return $feedbacks; - } - - /** - * Gets a stream context set up for SSL - * using our PEM file and passphrase - * - * @return resource - */ - protected function getStreamContext() - { - $ctx = stream_context_create(); - - stream_context_set_option($ctx, "ssl", "local_cert", $this->pem); - if (strlen($this->passphrase)) { - stream_context_set_option($ctx, "ssl", "passphrase", $this->passphrase); - } - - return $ctx; - } - -} diff --git a/Tests/Message/AndroidMessageTest.php b/Tests/Message/AndroidMessageTest.php deleted file mode 100644 index 62d8c76..0000000 --- a/Tests/Message/AndroidMessageTest.php +++ /dev/null @@ -1,114 +0,0 @@ -assertInstanceOf("RMS\PushNotificationsBundle\Message\MessageInterface", $msg); - $this->assertEquals(Types::OS_ANDROID_C2DM, $msg->getTargetOS()); - } - - public function testCoreBodyGeneratedOK() - { - $expected = array( - "registration_id" => "", - "collapse_key" => AndroidMessage::DEFAULT_COLLAPSE_KEY, - "data.message" => "", - ); - $msg = new AndroidMessage(); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testMessageAddedOK() - { - $expected = array( - "registration_id" => "", - "collapse_key" => AndroidMessage::DEFAULT_COLLAPSE_KEY, - "data.message" => "Foo", - ); - $msg = new AndroidMessage(); - $msg->setMessage("Foo"); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testNewCollapseKey() - { - $expected = array( - "registration_id" => "", - "collapse_key" => 123, - "data.message" => "", - ); - $msg = new AndroidMessage(); - $msg->setCollapseKey(123); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testRegistrationIDAddedToBody() - { - $expected = array( - "registration_id" => "ABC123", - "collapse_key" => AndroidMessage::DEFAULT_COLLAPSE_KEY, - "data.message" => "", - ); - $msg = new AndroidMessage(); - $msg->setDeviceIdentifier("ABC123"); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testCustomData() - { - $expected = array( - "registration_id" => "", - "collapse_key" => AndroidMessage::DEFAULT_COLLAPSE_KEY, - "data.message" => "", - "custom" => array("foo" => "bar"), - ); - $msg = new AndroidMessage(); - $msg->setData(array("custom" => array("foo" => "bar"))); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testTypeChangesBasedOnGCM() - { - $msg = new AndroidMessage(); - $this->assertEquals(Types::OS_ANDROID_C2DM, $msg->getTargetOS()); - $msg->setGCM(true); - $this->assertEquals(Types::OS_ANDROID_GCM, $msg->getTargetOS()); - } - - public function testSetIdentifierIsSingleEntryInGCMArray() - { - $msg = new AndroidMessage(); - $msg->setDeviceIdentifier("foo"); - $this->assertCount(1, $msg->getGCMIdentifiers()); - } - - public function testAddingGCMIdentifiers() - { - $msg = new AndroidMessage(); - $msg->addGCMIdentifier("foo"); - $msg->addGCMIdentifier("bar"); - $this->assertCount(2, $msg->getGCMIdentifiers()); - } - - public function testSetMessageIsReturnedInGetData() - { - $msg = new AndroidMessage(); - $message = 'Test message'; - $msg->setMessage($message); - $this->assertEquals(array('message' => $message), $msg->getData()); - - $msg->setData(array('id' => 10)); - $this->assertEquals(array('id' => 10, 'message' => $message), $msg->getData()); - - $msg->setData(array('message' => 'Other message')); - $this->assertEquals(array('message' => 'Other message'), $msg->getData()); - } -} diff --git a/Tests/Message/BlackberryMessageTest.php b/Tests/Message/BlackberryMessageTest.php deleted file mode 100644 index aacd5be..0000000 --- a/Tests/Message/BlackberryMessageTest.php +++ /dev/null @@ -1,32 +0,0 @@ -assertInstanceOf("RMS\PushNotificationsBundle\Message\MessageInterface", $msg); - $this->assertEquals(Types::OS_BLACKBERRY, $msg->getTargetOS()); - } - - public function testDefaultBody() - { - $expected = null; - $msg = new BlackberryMessage(); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testSettingBody() - { - $expected = "Foo"; - $msg = new BlackberryMessage(); - $msg->setMessage("Foo"); - $this->assertEquals($expected, $msg->getMessageBody()); - } -} diff --git a/Tests/Message/MacMessageTest.php b/Tests/Message/MacMessageTest.php deleted file mode 100644 index a5ffaa2..0000000 --- a/Tests/Message/MacMessageTest.php +++ /dev/null @@ -1,17 +0,0 @@ -assertInstanceOf("RMS\PushNotificationsBundle\Message\MessageInterface", $msg); - $this->assertEquals(Types::OS_MAC, $msg->getTargetOS()); - } -} diff --git a/Tests/Message/WindowsphoneMessageTest.php b/Tests/Message/WindowsphoneMessageTest.php deleted file mode 100644 index ed36ba5..0000000 --- a/Tests/Message/WindowsphoneMessageTest.php +++ /dev/null @@ -1,39 +0,0 @@ -assertInstanceOf("RMS\PushNotificationsBundle\Message\MessageInterface", $msg); - $this->assertEquals(Types::OS_WINDOWSPHONE, $msg->getTargetOS()); - } - - public function testDefaultBody() - { - $msg = new WindowsphoneMessage(); - $this->assertArrayHasKey("text1", $msg->getMessageBody()); - $this->assertArrayHasKey("text2", $msg->getMessageBody()); - } - - public function testSettingBody() - { - $expected = "Foo"; - $msg = new WindowsphoneMessage(); - $msg->setMessage("Foo"); - $msgBody = $msg->getMessageBody(); - $this->assertEquals($expected, $msgBody['text2']); - } - - public function testDefaultTarget() - { - $msg = new WindowsphoneMessage(); - $this->assertEquals(WindowsphoneMessage::TYPE_TOAST, $msg->getTarget()); - } -} diff --git a/Tests/Message/iOSMessageTest.php b/Tests/Message/iOSMessageTest.php deleted file mode 100644 index 7c3b5e6..0000000 --- a/Tests/Message/iOSMessageTest.php +++ /dev/null @@ -1,73 +0,0 @@ -assertInstanceOf("RMS\PushNotificationsBundle\Message\MessageInterface", $msg); - $this->assertEquals(Types::OS_IOS, $msg->getTargetOS()); - } - - public function testCoreBodyGeneratedOK() - { - $expected = array( - "aps" => array(), - ); - $msg = new iOSMessage(); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testAPSAlertAddedOK() - { - $expected = array( - "aps" => array( - "alert" => "Foo", - ), - ); - $msg = new iOSMessage(); - $msg->setMessage("Foo"); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testAPSBadgeAddedOK() - { - $expected = array( - "aps" => array( - "badge" => 1, - ), - ); - $msg = new iOSMessage(); - $msg->setAPSBadge(1); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testAPSSoundAddedOK() - { - $expected = array( - "aps" => array( - "sound" => "default", - ), - ); - $msg = new iOSMessage(); - $msg->setAPSSound("default"); - $this->assertEquals($expected, $msg->getMessageBody()); - } - - public function testCustomDataAddedOK() - { - $expected = array( - "aps" => array(), - "custom" => array("foo" => "bar"), - ); - $msg = new iOSMessage(); - $msg->setData(array("custom" => array("foo" => "bar"))); - $this->assertEquals($expected, $msg->getMessageBody()); - } -} diff --git a/composer.json b/composer.json index 7947f38..d63eaae 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "require": { "php": ">=5.3.0", "kriswallsmith/buzz": "*", - "psr/log": "^1.0" + "psr/log": "^1.0", + "richsage/rms-push-notifications": "*" }, "require-dev": { "symfony/symfony": "2.*"