Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
------
New release v2.0
Don't need anymore type the whole class name as argument.
Now you can use 'apn' 'fcm' 'gcm'
  • Loading branch information
Edujugon committed Jul 8, 2016
1 parent 9985ef4 commit 137e334
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 36 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
vendor/
composer.lock
.idea
src/Config/config.php
src/Config/iosCertificates/ck.pem
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ChangeLog

## v2.0

Now you can create the object with the push service provider only typing the name

$push = new PushNotification('apn');

Addition of a new Push service provider : FCM

$push = new PushNotification('fcm');

As previous versions, if you instance the class without argument. it will set GCM as default Push service provider

$push = new PushNotification();


## v1.0

Usage

$push = new PushNotification;

or for APN

$push = new PushNotification(new \Edujugon\PushNotification\Apn());
26 changes: 18 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

This is a lightly and easy to use package to send push notification.

## Instalation
## Installation

Update your composer.json file.
type in console:

"edujugon/push-notification": "dev-master"
composer require edujugon/push-notification


Or update your composer.json file.

"edujugon/push-notification": "^2.0.*"

Then

Expand Down Expand Up @@ -38,15 +43,20 @@ Publish the package's configuration file to the application's own config directo

By default it will use GCM as Push Service provider.

If you want to use APNS:
For APN Service:

$push = new PushNotification('apn');

For FCM Service:

$push = new PushNotification('fcm');

$push = new PushNotification(new \Edujugon\PushNotification\Apn());

### Push Service configuration

The default configuration for all Push service providers is located in Config/config.php

The default configuration parameters for **GCM** are :
The default configuration parameters for **GCM** and **FCM** are :

* priority => normal
* dry_run => false
Expand All @@ -60,7 +70,7 @@ You can dynamically update those values or adding new ones calling the method se
]);


The default configuration parameters for **APNS** are:
The default configuration parameters for **APN** are:

* certificate => __DIR__ . '/iosCertificates/yourCertificate.pem'
* passPhrase => 'MyPassPhrase'
Expand Down Expand Up @@ -90,7 +100,7 @@ GCM sample:
->setApiKey('Server-API-Key')
->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...]);

APNS sample:
APN sample:

$push->setMessage([
'aps' => [
Expand Down
10 changes: 5 additions & 5 deletions src/Apn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ class Apn extends PushService implements PushServiceInterface
*
* @var string
*/
private $sandbox_url = 'ssl://gateway.sandbox.push.apple.com:2195';
private $sandboxUrl = 'ssl://gateway.sandbox.push.apple.com:2195';

/**
* Url for production
*
* @var string
*/
private $production_url = 'ssl://gateway.push.apple.com:2195';
private $productionUrl = 'ssl://gateway.push.apple.com:2195';

/**
* Apn constructor.
*/
public function __construct()
{
$this->url = $this->production_url;
$this->url = $this->productionUrl;

$this->config = $this->initializeConfig('apn');
}
Expand All @@ -44,9 +44,9 @@ public function setConfig(array $config)
{
if($this->config['dry_run']){

$this->setUrl($this->sandbox_url);
$this->setUrl($this->sandboxUrl);

}else $this->setUrl($this->production_url);
}else $this->setUrl($this->productionUrl);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
'priority' => 'normal',
'dry_run' => false
],
'fcm' => [
'priority' => 'normal',
'dry_run' => false
],
'apn' => [
'certificate' => __DIR__ . '/iosCertificates/ck.pem',
'passPhrase' => '123456',
Expand Down
37 changes: 37 additions & 0 deletions src/Contracts/PushServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,53 @@
interface PushServiceInterface
{

/**
* Set the url to connect with the Push service provider.
*
* @param $url
* @return mixed
*/
function setUrl($url);

/**
* Set the API KEY to connect with the Push service provider.
*
* @param $api_key
* @return mixed
*/
function setApiKey($api_key);

/**
* Set the Push service provider configuration.
*
* @param array $config
* @return mixed
*/
function setConfig(array $config);

/**
* Set the Push Notification Response.
*
* @param $feedback
* @return mixed
*/
function setFeedback($feedback);

/**
* Send the notification
*
* @param array $deviceTokens
* @param array $message
* @return mixed
*/
function send(array $deviceTokens, array $message);

/**
* Retrieve the device tokes that couldn't receive the message from the push notification.
*
* @param array $devices_token
* @return mixed
*/
function getUnregisteredDeviceTokens(array $devices_token);

}
8 changes: 6 additions & 2 deletions src/Fcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
namespace Edujugon\PushNotification;


use GuzzleHttp\Client;

class Fcm extends Gcm
{

public function __construct()
{
parent::__construct();

$this->url = 'https://fcm.googleapis.com/fcm/send';

$this->config = $this->initializeConfig('fcm');

$this->client = new Client();
}
}
2 changes: 1 addition & 1 deletion src/Gcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function addRequestFields($deviceTokens,$message){

protected function addRequestHeaders(){
return [
'Authorization' => 'key=' . $this->api_key,
'Authorization' => 'key=' . $this->apiKey,
'Content-Type:' =>'application/json'
];
}
Expand Down
32 changes: 20 additions & 12 deletions src/PushNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace Edujugon\PushNotification;


use Edujugon\PushNotification\Contracts\PushServiceInterface;

class PushNotification
{

Expand All @@ -18,18 +16,25 @@ class PushNotification
*
* @var array
*/
protected $service_collection = [
protected $servicesList = [
'gcm' => Gcm::class,
'apn' => Apn::class,
'fcm' => Fcm::class
];

/**
* The default push service to use.
*
* @var string
*/
private $defaultServiceName = 'gcm';

/**
* Devices' Token where send the notification
*
* @var array
*/
protected $devices_token = [];
protected $deviceTokens = [];

/**
* data to be sent.
Expand All @@ -40,12 +45,15 @@ class PushNotification

/**
* PushNotification constructor.
* @param PushServiceInterface $service By default GCM
* @param String / a service name of the services list.
*/
public function __construct($service = null)
{
$this->service = !is_null($service) ? $this->service_collection[$service] : new Fcm;
var_dump($this->service);
if(!array_key_exists($service,$this->servicesList)) $service = $this->defaultServiceName;

$this->service = is_null($service) ? new $this->servicesList[$this->defaultServiceName]
: new $this->servicesList[$service];

}

/**
Expand All @@ -63,12 +71,12 @@ public function setMessage(array $data)


/**
* @param array/string $devices_token
* @param array/string $deviceTokens
* @return $this
*/
public function setDevicesToken($devices_token)
public function setDevicesToken($deviceTokens)
{
$this->devices_token = is_array($devices_token) ? $devices_token : array($devices_token);
$this->deviceTokens = is_array($deviceTokens) ? $deviceTokens : array($deviceTokens);

return $this;
}
Expand Down Expand Up @@ -102,7 +110,7 @@ public function setConfig(array $config)
*/
public function getUnregisteredDeviceTokens()
{
return $this->service->getUnregisteredDeviceTokens($this->devices_token);
return $this->service->getUnregisteredDeviceTokens($this->deviceTokens);
}

/**
Expand All @@ -113,7 +121,7 @@ public function getUnregisteredDeviceTokens()
*/
public function send(){

return $this->service->send($this->devices_token,$this->message);
return $this->service->send($this->deviceTokens,$this->message);

}

Expand Down
8 changes: 4 additions & 4 deletions src/PushService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class PushService
*
* @var string
*/
protected $api_key = '';
protected $apiKey = '';

/**
* Push Server Response
Expand All @@ -43,11 +43,11 @@ public function setUrl($url)
}

/**
* @param string $api_key
* @param string $apiKey
*/
public function setApiKey($api_key)
public function setApiKey($apiKey)
{
$this->api_key = $api_key;
$this->apiKey = $apiKey;
}

/**
Expand Down
32 changes: 29 additions & 3 deletions tests/PushNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

class PushNotificationTest extends PHPUnit_Framework_TestCase {

/** @test */
public function push_notification_instance_creation_without_argument_set_gcm_as_service()
{
$push = new PushNotification();

$this->assertInstanceOf('Edujugon\PushNotification\Gcm',$push->service);
}

/** @test */
public function assert_send_method_returns_an_stdClass_instance()
{
Expand Down Expand Up @@ -148,14 +156,32 @@ public function fcm_assert_send_method_returns_an_stdClass_instance()
$push = new PushNotification('fcm');

$push->setMessage(['message'=>'Hello World'])
->setApiKey('AIzaSyAjsu5h5TLe9_q33zn2Q4a84N9KyCxCB04')
->setDevicesToken(['dHoZVUu4T34:APA91bHJJxHTJMSNp95A3qAcMEbPqiS02UKAiXH0J8M-k7owtPf4XrW9k8QttT9pCLy_QzoxKQsJ4pwaSVEXHhowoPaqCPp1pvVsBZ6QUHoEtO_S9-Esel4N7nqeUypQ6ah8MKZKo6jl'])
->setApiKey('asdfasdffasdfasdfasdf')
->setDevicesToken(['asdfasefaefwefwerwerwer'])
->setConfig(['dry_run' => false]);

$response = $push->send();
var_dump($response);

$this->assertEquals('https://fcm.googleapis.com/fcm/send',$push->url);
$this->assertInstanceOf('stdClass',$response);

}

/** @test */
public function if_push_service_as_argument_is_not_valid_user_gcm_as_default()
{
$push = new PushNotification('asdf');

$this->assertInstanceOf('Edujugon\PushNotification\Gcm',$push->service);


}
/** @test */
public function get_available_push_service_list()
{
$push = new PushNotification();

$this->assertCount(3,$push->servicesList);
$this->assertInternalType('array',$push->servicesList);
}
}

0 comments on commit 137e334

Please sign in to comment.