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

Added Topic Creation and Subscription Functionality #114

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ $topicResponse->shouldRetry();
$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

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
],
];
10 changes: 9 additions & 1 deletion src/FCMServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaravelFCM;

use LaravelFCM\Sender\FCMGroup;
use LaravelFCM\Sender\FCMTopic;
use LaravelFCM\Sender\FCMSender;
use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -38,6 +39,13 @@ public function register()
return new FCMGroup($client, $url);
});

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

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

$this->app->bind('fcm.sender', function ($app) {
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
Expand All @@ -48,6 +56,6 @@ public function register()

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

namespace LaravelFCM\Facades;

use Illuminate\Support\Facades\Facade;

class FCMTopic extends Facade
{
protected static function getFacadeAccessor()
{
return 'fcm.topic';
}
}
75 changes: 75 additions & 0 deletions src/Request/TopicRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace LaravelFCM\Request;

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

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

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

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

/**
* GroupRequest constructor.
*
* @param $operation
* @param $notificationKeyName
* @param $notificationKey
* @param $registrationIds
*/
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,
];
}
}
85 changes: 85 additions & 0 deletions src/Sender/FCMTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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 $notificationKeyName
* @param array $registrationIds
*
* @return null|string notification_key
*/
public function createTopic($topic_id, $registration_id)
{
$request = new TopicRequest('create', $topic_id);
if(is_array($registration_id)){
return null;
}
$response = $this->client->request('post', $this->url.$registration_id."/rel/topics/".$topic_id, $request->build());

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

/**
* add subscription to a topic.
*
* @param $topic_id
* @param $recipients_tokens
* @return null|string notification_key
*/
public function subscribeTopic($topic_id, $recipients_tokens)
{
$request = new TopicRequest('subscribe', $topic_id, $recipients_tokens);
$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 $topic_id
* @param $recipients_tokens
* @return null|string notification_key
*/
public function unsubscribeTopic($topic_id, $recipients_tokens)
{
$request = new TopicRequest('unsubscribe', $topic_id, $recipients_tokens);
$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;
}
}