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

Adding Topic Creation And Subscription And Validating FCM Token #5

Merged
merged 1 commit into from
Oct 5, 2020
Merged
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,32 @@ $topicResponse->error());

```

#### Creating a Topic

```php
$token = 'device_id';
$topic_id = 'unique_topic_id'; //unique topic id.
// Save notification key in your database you must use it to send messages or for managing this group
$notification_key = FCMTopic::createTopic($topic_id, $token);
```

#### Subscribe to a Topic

```php
$recipients_tokens = ['device_id', '...'];
$topic_id = 'unique_topic_id';
$key = FCMTopic::subscribeTopic($topic_id, $recipients_tokens);
```

#### UnSubscribe to a Topic

```php
$recipients_tokens = ['device_id', '...'];
$topic_id = 'unique_topic_id';
$key = FCMTopic::unsubscribeTopic($topic_id, $recipients_tokens);
```


### Group Messages

#### Sending a Notification to a Group
Expand Down Expand Up @@ -408,6 +434,13 @@ $topics->topic('TopicA')
});
```

## Validating

### Validate FCM Token

```php
$isValid = FCMValidator::validateToken($token);
```

## Testing

Expand Down
1 change: 1 addition & 0 deletions config/fcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'sender_id' => env('FCM_SENDER_ID', 'Your sender id'),
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
'server_topic_url' => 'https://iid.googleapis.com/iid/v1/',
'timeout' => 30.0, // in second
],
];
21 changes: 20 additions & 1 deletion src/FCMServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Support\Str;
use LaravelFCM\Sender\FCMGroup;
use LaravelFCM\Sender\FCMTopic;
use LaravelFCM\Sender\FCMSender;
use Illuminate\Support\ServiceProvider;
use LaravelFCM\Validator\FCMValidator;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\NullHandler;
Expand Down Expand Up @@ -53,17 +55,34 @@ public function register()
return new FCMGroup($client, $url, $logger);
});

$this->app->bind('fcm.topic', function ($app) {
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_topic_url');
$logger = $app[ 'fcm.logger' ];

return new FCMTopic($client, $url,$logger);
});

$this->app->bind('fcm.sender', function ($app) {
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
$logger = $app[ 'fcm.logger' ];

return new FCMSender($client, $url, $logger);
});

$this->app->bind('fcm.validator',function ($app){
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
$logger = $app[ 'fcm.logger' ];


return new FCMValidator($client, $url, $logger);
});
}

public function provides()
{
return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger'];
return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger', 'fcm.topic' , 'fcm.validator'];
}
}
17 changes: 17 additions & 0 deletions src/Facades/FCMTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace LaravelFCM\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static bool createTopic(string $topicId, string $registrationId)
* @method static bool subscribeTopic(string $topicId, array|string $recipientsTokens)
* @method static bool unsubscribeTopic(string $topicId, array|string $recipientsTokens)
*/
class FCMTopic extends Facade {
protected static function getFacadeAccessor()
{
return 'fcm.topic';
}
}
15 changes: 15 additions & 0 deletions src/Facades/FCMValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LaravelFCM\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static bool validateToken(string $token)
*/
class FCMValidator extends Facade {
protected static function getFacadeAccessor()
{
return 'fcm.validator';
}
}
67 changes: 67 additions & 0 deletions src/Request/TopicRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace LaravelFCM\Request;

/**
* Class TopicRequest.
*/
class TopicRequest extends BaseRequest
{
/**
* @internal
*
* @var string
*/
protected $operation;

/**
* @internal
*
* @var string
*/
protected $topic_id;

/**
* @internal
*
* @var array
*/
protected $recipients_tokens;

/**
* TopicRequest constructor.
*
* @param string $operation The operation name
* @param string $topic_id The topic id
* @param array|string $recipients_tokens The tokens or the token
*/
public function __construct($operation, $topic_id, $recipients_tokens = [])
{
parent::__construct();

if (! is_array($recipients_tokens)){
$recipients_tokens = [$recipients_tokens];
}

$this->topic_id = $topic_id;
$this->recipients_tokens = $recipients_tokens;
$this->operation = $operation;
}

/**
* Build the header for the request.
*
* @return array
*/
protected function buildBody()
{
if ($this->operation === 'create') {
return [];
}

return [
'to' => '/topics/' . $this->topic_id,
'registration_tokens' => $this->recipients_tokens,
];
}
}
15 changes: 15 additions & 0 deletions src/Request/ValidateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace LaravelFCM\Request;


class ValidateRequest extends BaseRequest {

/**
* @inheritDoc
*/
protected function buildBody() {
return [];
}
}
86 changes: 86 additions & 0 deletions src/Sender/FCMTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace LaravelFCM\Sender;

use LaravelFCM\Request\TopicRequest;
use Psr\Http\Message\ResponseInterface;

/**
* Class FCMGroup.
*/
class FCMTopic extends HTTPSender {

private $add_subscription_url = 'https://iid.googleapis.com/iid/v1:batchAdd';
private $remove_subscription_url = 'https://iid.googleapis.com/iid/v1:batchRemove';

/**
* Create a topic.
*
* @param string $topicId
* @param string $registrationId
*
* @return bool
*/
public function createTopic($topicId, $registrationId)
{
$request = new TopicRequest('create', $topicId);
if (is_array($registrationId)) {
return null;
}
$response = $this->client->request('post', $this->url . $registrationId . '/rel/topics/' . $topicId, $request->build());


if ($this->isValidResponse($response)) {
return true;
}
return false;

}

/**
* Add subscription to a topic.
*
* @param string $topicId
* @param array|string $recipientsTokens
* @return bool
*/
public function subscribeTopic($topicId, $recipientsTokens)
{
$request = new TopicRequest('subscribe', $topicId, $recipientsTokens);
$response = $this->client->request('post', $this->add_subscription_url, $request->build());

if ($this->isValidResponse($response)) {
return true;
}
return false;
}

/**
* Remove subscription from a topic.
*
*
* @param string $topicId
* @param array|string $recipientsTokens
* @return bool
*/
public function unsubscribeTopic($topicId, $recipientsTokens)
{
$request = new TopicRequest('unsubscribe', $topicId, $recipientsTokens);
$response = $this->client->request('post', $this->remove_subscription_url, $request->build());

if ($this->isValidResponse($response)) {
return true;
}
return false;
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return bool
*/
public function isValidResponse(ResponseInterface $response)
{
return $response->getStatusCode() === 200;
}
}
32 changes: 32 additions & 0 deletions src/Validator/FCMValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace LaravelFCM\Validator;

use GuzzleHttp\ClientInterface;
use LaravelFCM\Request\ValidateRequest;
use LaravelFCM\Sender\HTTPSender;
use Monolog\Logger;
use \Exception;

class FCMValidator extends HTTPSender {
private $validate_token_url = 'https://iid.googleapis.com/iid/info/'; // + YOUR_APP_TOKEN_HERE

/**
* @param string $token
*
* @return bool
*/
public function validateToken(string $token) {
$request = new ValidateRequest();
try {
$build = $request->build();
if (isset($build['json'])) {
unset($build['json']);
}
$this->client->request('get', $this->validate_token_url . $token, $build);
return true;
}catch (Exception $e) {
return false;
}
}
}