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

Commit 344acd8

Browse files
committed
Merge #5 - Add Topics Creation And Subscription
2 parents 33c25d5 + f04fdc8 commit 344acd8

File tree

9 files changed

+286
-1
lines changed

9 files changed

+286
-1
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,32 @@ $topicResponse->error());
268268

269269
```
270270

271+
#### Creating a Topic
272+
273+
```php
274+
$token = 'device_id';
275+
$topic_id = 'unique_topic_id'; //unique topic id.
276+
// Save notification key in your database you must use it to send messages or for managing this group
277+
$notification_key = FCMTopic::createTopic($topic_id, $token);
278+
```
279+
280+
#### Subscribe to a Topic
281+
282+
```php
283+
$recipients_tokens = ['device_id', '...'];
284+
$topic_id = 'unique_topic_id';
285+
$key = FCMTopic::subscribeTopic($topic_id, $recipients_tokens);
286+
```
287+
288+
#### UnSubscribe to a Topic
289+
290+
```php
291+
$recipients_tokens = ['device_id', '...'];
292+
$topic_id = 'unique_topic_id';
293+
$key = FCMTopic::unsubscribeTopic($topic_id, $recipients_tokens);
294+
```
295+
296+
271297
### Group Messages
272298

273299
#### Sending a Notification to a Group
@@ -408,6 +434,13 @@ $topics->topic('TopicA')
408434
});
409435
```
410436

437+
## Validating
438+
439+
### Validate FCM Token
440+
441+
```php
442+
$isValid = FCMValidator::validateToken($token);
443+
```
411444

412445
## Testing
413446

config/fcm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'sender_id' => env('FCM_SENDER_ID', 'Your sender id'),
1010
'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
1111
'server_group_url' => 'https://android.googleapis.com/gcm/notification',
12+
'server_topic_url' => 'https://iid.googleapis.com/iid/v1/',
1213
'timeout' => 30.0, // in second
1314
],
1415
];

src/FCMServiceProvider.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Support\Str;
66
use LaravelFCM\Sender\FCMGroup;
7+
use LaravelFCM\Sender\FCMTopic;
78
use LaravelFCM\Sender\FCMSender;
89
use Illuminate\Support\ServiceProvider;
10+
use LaravelFCM\Validator\FCMValidator;
911
use Monolog\Logger;
1012
use Monolog\Handler\StreamHandler;
1113
use Monolog\Handler\NullHandler;
@@ -53,17 +55,34 @@ public function register()
5355
return new FCMGroup($client, $url, $logger);
5456
});
5557

58+
$this->app->bind('fcm.topic', function ($app) {
59+
$client = $app[ 'fcm.client' ];
60+
$url = $app[ 'config' ]->get('fcm.http.server_topic_url');
61+
$logger = $app[ 'fcm.logger' ];
62+
63+
return new FCMTopic($client, $url,$logger);
64+
});
65+
5666
$this->app->bind('fcm.sender', function ($app) {
5767
$client = $app[ 'fcm.client' ];
5868
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
5969
$logger = $app[ 'fcm.logger' ];
6070

6171
return new FCMSender($client, $url, $logger);
6272
});
73+
74+
$this->app->bind('fcm.validator',function ($app){
75+
$client = $app[ 'fcm.client' ];
76+
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
77+
$logger = $app[ 'fcm.logger' ];
78+
79+
80+
return new FCMValidator($client, $url, $logger);
81+
});
6382
}
6483

6584
public function provides()
6685
{
67-
return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger'];
86+
return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger', 'fcm.topic' , 'fcm.validator'];
6887
}
6988
}

src/Facades/FCMTopic.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace LaravelFCM\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static bool createTopic(string $topicId, string $registrationId)
9+
* @method static bool subscribeTopic(string $topicId, array|string $recipientsTokens)
10+
* @method static bool unsubscribeTopic(string $topicId, array|string $recipientsTokens)
11+
*/
12+
class FCMTopic extends Facade {
13+
protected static function getFacadeAccessor()
14+
{
15+
return 'fcm.topic';
16+
}
17+
}

src/Facades/FCMValidator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LaravelFCM\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static bool validateToken(string $token)
9+
*/
10+
class FCMValidator extends Facade {
11+
protected static function getFacadeAccessor()
12+
{
13+
return 'fcm.validator';
14+
}
15+
}

src/Request/TopicRequest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace LaravelFCM\Request;
4+
5+
/**
6+
* Class TopicRequest.
7+
*/
8+
class TopicRequest extends BaseRequest
9+
{
10+
/**
11+
* @internal
12+
*
13+
* @var string
14+
*/
15+
protected $operation;
16+
17+
/**
18+
* @internal
19+
*
20+
* @var string
21+
*/
22+
protected $topic_id;
23+
24+
/**
25+
* @internal
26+
*
27+
* @var array
28+
*/
29+
protected $recipients_tokens;
30+
31+
/**
32+
* TopicRequest constructor.
33+
*
34+
* @param string $operation The operation name
35+
* @param string $topic_id The topic id
36+
* @param array|string $recipients_tokens The tokens or the token
37+
*/
38+
public function __construct($operation, $topic_id, $recipients_tokens = [])
39+
{
40+
parent::__construct();
41+
42+
if (! is_array($recipients_tokens)){
43+
$recipients_tokens = [$recipients_tokens];
44+
}
45+
46+
$this->topic_id = $topic_id;
47+
$this->recipients_tokens = $recipients_tokens;
48+
$this->operation = $operation;
49+
}
50+
51+
/**
52+
* Build the header for the request.
53+
*
54+
* @return array
55+
*/
56+
protected function buildBody()
57+
{
58+
if ($this->operation === 'create') {
59+
return [];
60+
}
61+
62+
return [
63+
'to' => '/topics/' . $this->topic_id,
64+
'registration_tokens' => $this->recipients_tokens,
65+
];
66+
}
67+
}

src/Request/ValidateRequest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
4+
namespace LaravelFCM\Request;
5+
6+
7+
class ValidateRequest extends BaseRequest {
8+
9+
/**
10+
* @inheritDoc
11+
*/
12+
protected function buildBody() {
13+
return [];
14+
}
15+
}

src/Sender/FCMTopic.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace LaravelFCM\Sender;
4+
5+
use LaravelFCM\Request\TopicRequest;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* Class FCMGroup.
10+
*/
11+
class FCMTopic extends HTTPSender {
12+
13+
private $add_subscription_url = 'https://iid.googleapis.com/iid/v1:batchAdd';
14+
private $remove_subscription_url = 'https://iid.googleapis.com/iid/v1:batchRemove';
15+
16+
/**
17+
* Create a topic.
18+
*
19+
* @param string $topicId
20+
* @param string $registrationId
21+
*
22+
* @return bool
23+
*/
24+
public function createTopic($topicId, $registrationId)
25+
{
26+
$request = new TopicRequest('create', $topicId);
27+
if (is_array($registrationId)) {
28+
return null;
29+
}
30+
$response = $this->client->request('post', $this->url . $registrationId . '/rel/topics/' . $topicId, $request->build());
31+
32+
33+
if ($this->isValidResponse($response)) {
34+
return true;
35+
}
36+
return false;
37+
38+
}
39+
40+
/**
41+
* Add subscription to a topic.
42+
*
43+
* @param string $topicId
44+
* @param array|string $recipientsTokens
45+
* @return bool
46+
*/
47+
public function subscribeTopic($topicId, $recipientsTokens)
48+
{
49+
$request = new TopicRequest('subscribe', $topicId, $recipientsTokens);
50+
$response = $this->client->request('post', $this->add_subscription_url, $request->build());
51+
52+
if ($this->isValidResponse($response)) {
53+
return true;
54+
}
55+
return false;
56+
}
57+
58+
/**
59+
* Remove subscription from a topic.
60+
*
61+
*
62+
* @param string $topicId
63+
* @param array|string $recipientsTokens
64+
* @return bool
65+
*/
66+
public function unsubscribeTopic($topicId, $recipientsTokens)
67+
{
68+
$request = new TopicRequest('unsubscribe', $topicId, $recipientsTokens);
69+
$response = $this->client->request('post', $this->remove_subscription_url, $request->build());
70+
71+
if ($this->isValidResponse($response)) {
72+
return true;
73+
}
74+
return false;
75+
}
76+
77+
/**
78+
* @param \Psr\Http\Message\ResponseInterface $response
79+
*
80+
* @return bool
81+
*/
82+
public function isValidResponse(ResponseInterface $response)
83+
{
84+
return $response->getStatusCode() === 200;
85+
}
86+
}

src/Validator/FCMValidator.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace LaravelFCM\Validator;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use LaravelFCM\Request\ValidateRequest;
7+
use LaravelFCM\Sender\HTTPSender;
8+
use Monolog\Logger;
9+
use \Exception;
10+
11+
class FCMValidator extends HTTPSender {
12+
private $validate_token_url = 'https://iid.googleapis.com/iid/info/'; // + YOUR_APP_TOKEN_HERE
13+
14+
/**
15+
* @param string $token
16+
*
17+
* @return bool
18+
*/
19+
public function validateToken(string $token) {
20+
$request = new ValidateRequest();
21+
try {
22+
$build = $request->build();
23+
if (isset($build['json'])) {
24+
unset($build['json']);
25+
}
26+
$this->client->request('get', $this->validate_token_url . $token, $build);
27+
return true;
28+
}catch (Exception $e) {
29+
return false;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)