diff --git a/.gitignore b/.gitignore index 6b6e772..03a2edd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ vendor/ composer.lock .idea -src/Config/config.php src/Config/iosCertificates/ck.pem \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ad20d09 --- /dev/null +++ b/CHANGELOG.md @@ -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()); diff --git a/readme.md b/readme.md index fab4d8f..10567b0 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 @@ -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' @@ -90,7 +100,7 @@ GCM sample: ->setApiKey('Server-API-Key') ->setDevicesToken(['deviceToken1','deviceToken2','deviceToken3'...]); -APNS sample: +APN sample: $push->setMessage([ 'aps' => [ diff --git a/src/Apn.php b/src/Apn.php index 832eeff..5968151 100644 --- a/src/Apn.php +++ b/src/Apn.php @@ -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'); } @@ -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); } } diff --git a/src/Config/config.php b/src/Config/config.php index 10bebe8..d13dd9a 100644 --- a/src/Config/config.php +++ b/src/Config/config.php @@ -5,6 +5,10 @@ 'priority' => 'normal', 'dry_run' => false ], + 'fcm' => [ + 'priority' => 'normal', + 'dry_run' => false + ], 'apn' => [ 'certificate' => __DIR__ . '/iosCertificates/ck.pem', 'passPhrase' => '123456', diff --git a/src/Contracts/PushServiceInterface.php b/src/Contracts/PushServiceInterface.php index cfa8ef7..f65aead 100644 --- a/src/Contracts/PushServiceInterface.php +++ b/src/Contracts/PushServiceInterface.php @@ -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); } \ No newline at end of file diff --git a/src/Fcm.php b/src/Fcm.php index c1425de..7d8723b 100644 --- a/src/Fcm.php +++ b/src/Fcm.php @@ -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(); } } \ No newline at end of file diff --git a/src/Gcm.php b/src/Gcm.php index 0e1d114..42a68c6 100644 --- a/src/Gcm.php +++ b/src/Gcm.php @@ -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' ]; } diff --git a/src/PushNotification.php b/src/PushNotification.php index 9f50161..a84d93c 100644 --- a/src/PushNotification.php +++ b/src/PushNotification.php @@ -2,8 +2,6 @@ namespace Edujugon\PushNotification; -use Edujugon\PushNotification\Contracts\PushServiceInterface; - class PushNotification { @@ -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. @@ -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]; + } /** @@ -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; } @@ -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); } /** @@ -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); } diff --git a/src/PushService.php b/src/PushService.php index 4f4310c..01733a4 100644 --- a/src/PushService.php +++ b/src/PushService.php @@ -26,7 +26,7 @@ abstract class PushService * * @var string */ - protected $api_key = ''; + protected $apiKey = ''; /** * Push Server Response @@ -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; } /** diff --git a/tests/PushNotificationTest.php b/tests/PushNotificationTest.php index b3c14bb..27a81a3 100644 --- a/tests/PushNotificationTest.php +++ b/tests/PushNotificationTest.php @@ -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() { @@ -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); + } } \ No newline at end of file