Skip to content

Commit

Permalink
Merge pull request #4 from redjanym/develop
Browse files Browse the repository at this point in the history
New Feature "Topic Notification", minor bug fixes and new documentation wiki
  • Loading branch information
redjanym authored Dec 2, 2016
2 parents b02a43e + 1168b88 commit 4ab83d7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 209 deletions.
4 changes: 1 addition & 3 deletions Entity/DeviceNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!');
}
}
}
31 changes: 31 additions & 0 deletions Entity/TopicNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RedjanYm\FCMBundle\Entity;


class TopicNotification extends Notification implements \RedjanYm\FCMBundle\Model\TopicNotification
{
private $topic;

/**
* @inheritdoc
*/
public function setTopic($topic)
{
$this->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!');
}
}
}
76 changes: 70 additions & 6 deletions FCMClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,9 +38,18 @@ class FCMClient
*/
public function __construct(Client $client)
{
$client->injectGuzzleHttpClient(new \GuzzleHttp\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();
Expand All @@ -52,22 +61,77 @@ 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;
}

/**
* 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)
{
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(
Expand Down
18 changes: 18 additions & 0 deletions Model/TopicNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace RedjanYm\FCMBundle\Model;

interface TopicNotification
{
/**
* @param string $topic
*
* @return TopicNotification
*/
public function setTopic($topic);

/**
* @return string
*/
public function getTopic();
}
107 changes: 4 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,106 +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
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...

new RedjanYm\FCMBundle\RedjanYmFCMBundle(),
);

// ...
}

// ...
}
```

Step 3: Configuration
---------------------

On your `app/config.yml` file add this configuration and insert your Firebase API Key that you can generate in the Firebase Console.

```yaml
redjan_ym_fcm:
firebase_api_key: sdadasdasdasdasd245sadas5d4a8sd2d5as4d8s
```
Step 4: Usage
-------------
* Get the FCM client from the container.
```php
$fcmClient = $this->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
For more detailed information about **Instalation** and **Usage** please go to bundle's [Wiki](https://github.com/redjanym/FCMBundle/wiki)
97 changes: 0 additions & 97 deletions Resources/README.md

This file was deleted.

0 comments on commit 4ab83d7

Please sign in to comment.