From 686049df30083e6e2680a9811529403ccd13ebbf Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Thu, 1 Dec 2016 16:18:05 +0100 Subject: [PATCH 1/9] Add Send Topic Notifications feature --- Entity/DeviceNotification.php | 4 +- Entity/TopicNotification.php | 31 +++++++++++ FCMClient.php | 41 +++++++++++++-- Model/TopicNotification.php | 18 +++++++ README.md | 7 ++- Resources/README.md | 97 ----------------------------------- 6 files changed, 90 insertions(+), 108 deletions(-) create mode 100644 Entity/TopicNotification.php create mode 100644 Model/TopicNotification.php delete mode 100755 Resources/README.md diff --git a/Entity/DeviceNotification.php b/Entity/DeviceNotification.php index c28c875..9048bfc 100644 --- a/Entity/DeviceNotification.php +++ b/Entity/DeviceNotification.php @@ -2,8 +2,6 @@ namespace RedjanYm\FCMBundle\Entity; -use Symfony\Component\Console\Exception\InvalidArgumentException; - class DeviceNotification extends Notification implements \RedjanYm\FCMBundle\Model\DeviceNotification { private $deviceToken; @@ -30,7 +28,7 @@ public function getDeviceToken() if ($this->deviceToken) { return $this->deviceToken; } else { - throw new InvalidArgumentException('The Mobile Notification must have a Device Token!'); + throw new \InvalidArgumentException('The Mobile Notification must have a Device Token!'); } } } diff --git a/Entity/TopicNotification.php b/Entity/TopicNotification.php new file mode 100644 index 0000000..8226579 --- /dev/null +++ b/Entity/TopicNotification.php @@ -0,0 +1,31 @@ +topic = $topic; + + return $this; + } + + /** + * @inheritdoc + */ + public function getTopic() + { + if($this->topic){ + return $this->topic; + } else { + throw new \InvalidArgumentException('The Topic Notification must have a Topic!'); + } + } +} \ No newline at end of file diff --git a/FCMClient.php b/FCMClient.php index 5e23b74..d8f31ce 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -17,7 +17,7 @@ use sngrl\PhpFirebaseCloudMessaging\Message; use sngrl\PhpFirebaseCloudMessaging\Notification; use sngrl\PhpFirebaseCloudMessaging\Recipient\Device; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic; /** * The FCMBundle primary class. @@ -41,6 +41,14 @@ public function __construct(Client $client) $this->client = $client; } + /** + * Create a notification of type Device Notification + * + * @param null $title + * @param null $body + * @param null $token + * @return DeviceNotification + */ public function createDeviceNotification($title = null, $body = null, $token = null) { $notification = new DeviceNotification(); @@ -52,6 +60,25 @@ public function createDeviceNotification($title = null, $body = null, $token = n return $notification; } + /** + * Create a notification of type Topic + * + * @param null $title + * @param null $body + * @param null $topic + * @return TopicNotification + */ + public function createTopicNotification($title = null, $body = null, $topic = null) + { + $notification = new TopicNotification(); + $notification + ->setTitle($title) + ->setBody($body) + ->setTopic($topic); + + return $notification; + } + /** * @param DeviceNotification | TopicNotification $notification * @@ -59,15 +86,21 @@ public function createDeviceNotification($title = null, $body = null, $token = n */ public function sendNotification($notification) { - if (!$notification instanceof DeviceNotification) { - throw new NotFoundHttpException('Notification must be of type DeviceNotification'); + if (!$notification instanceof DeviceNotification || !$notification instanceof TopicNotification) { + throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } + $this->client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); $message = new Message(); $message->setPriority($notification->getPriority()); - $message->addRecipient(new Device($notification->getDeviceToken())); + // Check for the type of Notification + if($notification instanceof DeviceNotification){ + $message->addRecipient(new Device($notification->getDeviceToken())); + } else if ($notification instanceof TopicNotification) { + $message->addRecipient(new Topic($notification->getTopic())); + } $message ->setNotification( diff --git a/Model/TopicNotification.php b/Model/TopicNotification.php new file mode 100644 index 0000000..c6da328 --- /dev/null +++ b/Model/TopicNotification.php @@ -0,0 +1,18 @@ +createDeviceNotification( In case you need to send extra data or set other notification properties the Notification Entity supports a set of setters and getters like: ``` $notification->setData($array); and $notification->getData(); - $notification->setPriority('high'); // Excepts 2 priorities, high(default) and low - $notification->setIcon('name of icon located in the native mobile app'); ``` @@ -88,7 +86,7 @@ You could also modify the arguments passed in the `createDeviceNotification` met * Send notification ``` -$fcmClient->sendNotification($notification); + $fcmClient->sendNotification($notification); ``` The request of sending the notification is a HTTP Synchronous Request. @@ -103,4 +101,5 @@ The request of sending the notification is a HTTP Synchronous Request. ----------- **License** -FCMBundle is licensed under the MIT license \ No newline at end of file + +FCMBundle is licensed under the [MIT license](LICENSE) \ No newline at end of file diff --git a/Resources/README.md b/Resources/README.md deleted file mode 100755 index 010394c..0000000 --- a/Resources/README.md +++ /dev/null @@ -1,97 +0,0 @@ -Installation -============ - -Step 1: Download the Bundle ---------------------------- - -Execute the following command to download the latest stable version of this bundle: - -```bash -$ composer require RedjanYm/fcm-bundle "^1.0" -``` - -This command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation. - -Step 2: Enable the Bundle -------------------------- - -Then, enable the bundle by adding it to the list of registered bundles in the `app/AppKernel.php` file of your project: - -```php -getContainer()->get('redjan_ym_fcm.client'); -``` - -* Create a Notification -For now FCMBundle supports only Device Notifications. Topic Notifications will be added in the future versions. - -``` -$notification = $fcmClient->createDeviceNotification( - 'Title of Notification', - 'Body of Notification', - 'Firebase Token of the device who will recive the notification' - ); -``` -In case you need to send extra data or set other notification properties the Notification Entity suports a set of setters and getters like: -``` - $notification->setData($array); and $notification->getData(); - - $notification->setPriority('high'); // Exepts 2 priorities, high(default) and low - - $notification->setIcon('name of icon located in the native mobile app'); -``` - -And also modify use setters and getters for the arguments passed in the `createDeviceNotification` method. -``` - $notifiaction->setTitle('string'); - $notifiaction->setBody('text'); - $notifiaction->setDeviceToken('string'); -``` - -**The only required field is the Device Token** - -* Send notification -``` -$fcmClient->sendNotification($notification); -``` - -The request of sending the notification is a Synchronous Request. - -**The Asynchronous requests will be implemented in the next version!** \ No newline at end of file From b6ab6c82df0d3e65abd68f1733adc5f5e2e83578 Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Thu, 1 Dec 2016 18:55:50 +0100 Subject: [PATCH 2/9] Add "ubscribe and unsubscribe devices in a Topic" feature --- FCMClient.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/FCMClient.php b/FCMClient.php index d8f31ce..c25c458 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -79,10 +79,42 @@ public function createTopicNotification($title = null, $body = null, $topic = nu return $notification; } + /** + * Subscribe devices to a Topic + * + * @param null $topicId + * @param array $deviceTokens + * @return \Psr\Http\Message\ResponseInterface + */ + public function subscribeDevicesToTopic($topicId = null, $deviceTokens = array()) + { + if(!$topicId || empty($deviceTokens)){ + throw new \InvalidArgumentException("Please check arguments!"); + } + + return $this->client->addTopicSubscription($topicId, $deviceTokens); + } + + /** + * Remove devices from a Topic + * + * @param null $topicId + * @param array $deviceTokens + * @return \Psr\Http\Message\ResponseInterface + */ + public function removeDevicesFromTopic($topicId = null, $deviceTokens = array()) + { + if(!$topicId || empty($deviceTokens)){ + throw new \InvalidArgumentException("Please check arguments!"); + } + + return $this->client->removeTopicSubscription($topicId, $deviceTokens); + } + /** * @param DeviceNotification | TopicNotification $notification * - * @return Client + * @return \Psr\Http\Message\ResponseInterface */ public function sendNotification($notification) { From 6fdf075bff7e6975333907b376cc327c3d5cd714 Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Fri, 2 Dec 2016 21:17:26 +0100 Subject: [PATCH 3/9] Fix error --- FCMClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FCMClient.php b/FCMClient.php index c25c458..db63aa4 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -118,7 +118,7 @@ public function removeDevicesFromTopic($topicId = null, $deviceTokens = array()) */ public function sendNotification($notification) { - if (!$notification instanceof DeviceNotification || !$notification instanceof TopicNotification) { + if (!$notification instanceof DeviceNotification && !$notification instanceof TopicNotification) { throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } From c14a25858844df27ba232141099790a0329ba3ab Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Fri, 2 Dec 2016 21:54:27 +0100 Subject: [PATCH 4/9] Fix error in Topic Notifications --- FCMClient.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/FCMClient.php b/FCMClient.php index db63aa4..bbe3365 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -38,6 +38,7 @@ class FCMClient */ public function __construct(Client $client) { + $client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); $this->client = $client; } @@ -122,8 +123,6 @@ public function sendNotification($notification) throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } - $this->client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); - $message = new Message(); $message->setPriority($notification->getPriority()); From 2d2a24fa887466bec63cf8b4a40a0e07c689c545 Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Thu, 1 Dec 2016 16:18:05 +0100 Subject: [PATCH 5/9] Add Send Topic Notifications feature --- Entity/DeviceNotification.php | 4 +- Entity/TopicNotification.php | 31 +++++++++++ FCMClient.php | 41 +++++++++++++-- Model/TopicNotification.php | 18 +++++++ README.md | 7 ++- Resources/README.md | 97 ----------------------------------- 6 files changed, 90 insertions(+), 108 deletions(-) create mode 100644 Entity/TopicNotification.php create mode 100644 Model/TopicNotification.php delete mode 100755 Resources/README.md diff --git a/Entity/DeviceNotification.php b/Entity/DeviceNotification.php index c28c875..9048bfc 100644 --- a/Entity/DeviceNotification.php +++ b/Entity/DeviceNotification.php @@ -2,8 +2,6 @@ namespace RedjanYm\FCMBundle\Entity; -use Symfony\Component\Console\Exception\InvalidArgumentException; - class DeviceNotification extends Notification implements \RedjanYm\FCMBundle\Model\DeviceNotification { private $deviceToken; @@ -30,7 +28,7 @@ public function getDeviceToken() if ($this->deviceToken) { return $this->deviceToken; } else { - throw new InvalidArgumentException('The Mobile Notification must have a Device Token!'); + throw new \InvalidArgumentException('The Mobile Notification must have a Device Token!'); } } } diff --git a/Entity/TopicNotification.php b/Entity/TopicNotification.php new file mode 100644 index 0000000..8226579 --- /dev/null +++ b/Entity/TopicNotification.php @@ -0,0 +1,31 @@ +topic = $topic; + + return $this; + } + + /** + * @inheritdoc + */ + public function getTopic() + { + if($this->topic){ + return $this->topic; + } else { + throw new \InvalidArgumentException('The Topic Notification must have a Topic!'); + } + } +} \ No newline at end of file diff --git a/FCMClient.php b/FCMClient.php index 5e23b74..d8f31ce 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -17,7 +17,7 @@ use sngrl\PhpFirebaseCloudMessaging\Message; use sngrl\PhpFirebaseCloudMessaging\Notification; use sngrl\PhpFirebaseCloudMessaging\Recipient\Device; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic; /** * The FCMBundle primary class. @@ -41,6 +41,14 @@ public function __construct(Client $client) $this->client = $client; } + /** + * Create a notification of type Device Notification + * + * @param null $title + * @param null $body + * @param null $token + * @return DeviceNotification + */ public function createDeviceNotification($title = null, $body = null, $token = null) { $notification = new DeviceNotification(); @@ -52,6 +60,25 @@ public function createDeviceNotification($title = null, $body = null, $token = n return $notification; } + /** + * Create a notification of type Topic + * + * @param null $title + * @param null $body + * @param null $topic + * @return TopicNotification + */ + public function createTopicNotification($title = null, $body = null, $topic = null) + { + $notification = new TopicNotification(); + $notification + ->setTitle($title) + ->setBody($body) + ->setTopic($topic); + + return $notification; + } + /** * @param DeviceNotification | TopicNotification $notification * @@ -59,15 +86,21 @@ public function createDeviceNotification($title = null, $body = null, $token = n */ public function sendNotification($notification) { - if (!$notification instanceof DeviceNotification) { - throw new NotFoundHttpException('Notification must be of type DeviceNotification'); + if (!$notification instanceof DeviceNotification || !$notification instanceof TopicNotification) { + throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } + $this->client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); $message = new Message(); $message->setPriority($notification->getPriority()); - $message->addRecipient(new Device($notification->getDeviceToken())); + // Check for the type of Notification + if($notification instanceof DeviceNotification){ + $message->addRecipient(new Device($notification->getDeviceToken())); + } else if ($notification instanceof TopicNotification) { + $message->addRecipient(new Topic($notification->getTopic())); + } $message ->setNotification( diff --git a/Model/TopicNotification.php b/Model/TopicNotification.php new file mode 100644 index 0000000..c6da328 --- /dev/null +++ b/Model/TopicNotification.php @@ -0,0 +1,18 @@ +createDeviceNotification( In case you need to send extra data or set other notification properties the Notification Entity supports a set of setters and getters like: ``` $notification->setData($array); and $notification->getData(); - $notification->setPriority('high'); // Excepts 2 priorities, high(default) and low - $notification->setIcon('name of icon located in the native mobile app'); ``` @@ -88,7 +86,7 @@ You could also modify the arguments passed in the `createDeviceNotification` met * Send notification ``` -$fcmClient->sendNotification($notification); + $fcmClient->sendNotification($notification); ``` The request of sending the notification is a HTTP Synchronous Request. @@ -103,4 +101,5 @@ The request of sending the notification is a HTTP Synchronous Request. ----------- **License** -FCMBundle is licensed under the MIT license \ No newline at end of file + +FCMBundle is licensed under the [MIT license](LICENSE) \ No newline at end of file diff --git a/Resources/README.md b/Resources/README.md deleted file mode 100755 index 010394c..0000000 --- a/Resources/README.md +++ /dev/null @@ -1,97 +0,0 @@ -Installation -============ - -Step 1: Download the Bundle ---------------------------- - -Execute the following command to download the latest stable version of this bundle: - -```bash -$ composer require RedjanYm/fcm-bundle "^1.0" -``` - -This command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation. - -Step 2: Enable the Bundle -------------------------- - -Then, enable the bundle by adding it to the list of registered bundles in the `app/AppKernel.php` file of your project: - -```php -getContainer()->get('redjan_ym_fcm.client'); -``` - -* Create a Notification -For now FCMBundle supports only Device Notifications. Topic Notifications will be added in the future versions. - -``` -$notification = $fcmClient->createDeviceNotification( - 'Title of Notification', - 'Body of Notification', - 'Firebase Token of the device who will recive the notification' - ); -``` -In case you need to send extra data or set other notification properties the Notification Entity suports a set of setters and getters like: -``` - $notification->setData($array); and $notification->getData(); - - $notification->setPriority('high'); // Exepts 2 priorities, high(default) and low - - $notification->setIcon('name of icon located in the native mobile app'); -``` - -And also modify use setters and getters for the arguments passed in the `createDeviceNotification` method. -``` - $notifiaction->setTitle('string'); - $notifiaction->setBody('text'); - $notifiaction->setDeviceToken('string'); -``` - -**The only required field is the Device Token** - -* Send notification -``` -$fcmClient->sendNotification($notification); -``` - -The request of sending the notification is a Synchronous Request. - -**The Asynchronous requests will be implemented in the next version!** \ No newline at end of file From 09fde57335ad2f0262fee8dd5ec976232acfbbe1 Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Thu, 1 Dec 2016 18:55:50 +0100 Subject: [PATCH 6/9] Add "ubscribe and unsubscribe devices in a Topic" feature --- FCMClient.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/FCMClient.php b/FCMClient.php index d8f31ce..c25c458 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -79,10 +79,42 @@ public function createTopicNotification($title = null, $body = null, $topic = nu return $notification; } + /** + * Subscribe devices to a Topic + * + * @param null $topicId + * @param array $deviceTokens + * @return \Psr\Http\Message\ResponseInterface + */ + public function subscribeDevicesToTopic($topicId = null, $deviceTokens = array()) + { + if(!$topicId || empty($deviceTokens)){ + throw new \InvalidArgumentException("Please check arguments!"); + } + + return $this->client->addTopicSubscription($topicId, $deviceTokens); + } + + /** + * Remove devices from a Topic + * + * @param null $topicId + * @param array $deviceTokens + * @return \Psr\Http\Message\ResponseInterface + */ + public function removeDevicesFromTopic($topicId = null, $deviceTokens = array()) + { + if(!$topicId || empty($deviceTokens)){ + throw new \InvalidArgumentException("Please check arguments!"); + } + + return $this->client->removeTopicSubscription($topicId, $deviceTokens); + } + /** * @param DeviceNotification | TopicNotification $notification * - * @return Client + * @return \Psr\Http\Message\ResponseInterface */ public function sendNotification($notification) { From e56bb2346192942a738500c2104503164136a59d Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Fri, 2 Dec 2016 21:17:26 +0100 Subject: [PATCH 7/9] Fix error --- FCMClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FCMClient.php b/FCMClient.php index c25c458..db63aa4 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -118,7 +118,7 @@ public function removeDevicesFromTopic($topicId = null, $deviceTokens = array()) */ public function sendNotification($notification) { - if (!$notification instanceof DeviceNotification || !$notification instanceof TopicNotification) { + if (!$notification instanceof DeviceNotification && !$notification instanceof TopicNotification) { throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } From 6a73f5a42ebc1f882e8110145617805e2851ba99 Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Fri, 2 Dec 2016 21:54:27 +0100 Subject: [PATCH 8/9] Fix error in Topic Notifications --- FCMClient.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/FCMClient.php b/FCMClient.php index db63aa4..bbe3365 100644 --- a/FCMClient.php +++ b/FCMClient.php @@ -38,6 +38,7 @@ class FCMClient */ public function __construct(Client $client) { + $client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); $this->client = $client; } @@ -122,8 +123,6 @@ public function sendNotification($notification) throw new \InvalidArgumentException('Notification must be of type DeviceNotification or TopicNotification'); } - $this->client->injectGuzzleHttpClient(new \GuzzleHttp\Client()); - $message = new Message(); $message->setPriority($notification->getPriority()); From 1168b88f814c9f0f1cf31d3849b0891d3a491c9d Mon Sep 17 00:00:00 2001 From: Redjan Ymeraj Date: Fri, 2 Dec 2016 23:00:30 +0100 Subject: [PATCH 9/9] Update README.md --- README.md | 106 +++--------------------------------------------------- 1 file changed, 4 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 95045b8..0a2fb5c 100755 --- a/README.md +++ b/README.md @@ -1,105 +1,7 @@ -Installation -============ +## FCMBundle -Step 1: Download the Bundle ---------------------------- +This bundle's purpose is to make possible sending native notifications in mobile devices(Android and iOS) that use Google's **Firebase Cloud Messaging** service from a **Symfony2 Web App**. -Execute the following command to download the latest stable version of this bundle: +---- -```bash -$ composer require redjanym/fcm-bundle -``` - -This command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation. - -Step 2: Enable the Bundle -------------------------- - -Then, enable the bundle by adding it to the list of registered bundles in the `app/AppKernel.php` file of your project: - -```php -getContainer()->get('redjan_ym_fcm.client'); -``` - -* Create a Notification. For now FCMBundle supports only Device Notifications. Topic Notifications will be added in the future versions. - -``` -$notification = $fcmClient->createDeviceNotification( - 'Title of Notification', - 'Body of Notification', - 'Firebase Token of the device who will recive the notification' - ); -``` -In case you need to send extra data or set other notification properties the Notification Entity supports a set of setters and getters like: -``` - $notification->setData($array); and $notification->getData(); - $notification->setPriority('high'); // Excepts 2 priorities, high(default) and low - $notification->setIcon('name of icon located in the native mobile app'); -``` - -You could also modify the arguments passed in the `createDeviceNotification` method. -``` - $notifiaction->setTitle('string'); - $notifiaction->setBody('text'); - $notifiaction->setDeviceToken('string'); -``` - -**The only required field is the Device Token** - -* Send notification -``` - $fcmClient->sendNotification($notification); -``` - -The request of sending the notification is a HTTP Synchronous Request. - -> The Asynchronous requests will be implemented in the next version! - - ----------- - - -> This Bundle is a wrapper for this [Firebase Cloud Messaging](https://github.com/sngrl/php-firebase-cloud-messaging) php library - ------------ -**License** - -FCMBundle is licensed under the [MIT license](LICENSE) \ No newline at end of file +For more detailed information about **Instalation** and **Usage** please go to bundle's [Wiki](https://github.com/redjanym/FCMBundle/wiki)