Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
------
constructor argument changed. now with a string. (gcm,apn or fcm)
  • Loading branch information
Edujugon committed Jul 7, 2016
1 parent cbc8076 commit 9985ef4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/Fcm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Edujugon\PushNotification;


class Fcm extends Gcm
{

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

$this->url = 'https://fcm.googleapis.com/fcm/send';
}
}
4 changes: 2 additions & 2 deletions src/Gcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public function getUnregisteredDeviceTokens(array $devices_token)
return [];
}

private function addRequestFields($deviceTokens,$message){
protected function addRequestFields($deviceTokens,$message){
return array_merge($this->config,[
'registration_ids' => $deviceTokens,
'data' => $message
]);
}

private function addRequestHeaders(){
protected function addRequestHeaders(){
return [
'Authorization' => 'key=' . $this->api_key,
'Content-Type:' =>'application/json'
Expand Down
16 changes: 14 additions & 2 deletions src/PushNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ class PushNotification
*/
protected $service;

/**
* List of the available Push service providers
*
* @var array
*/
protected $service_collection = [
'gcm' => Gcm::class,
'apn' => Apn::class,
'fcm' => Fcm::class
];

/**
* Devices' Token where send the notification
*
Expand All @@ -31,9 +42,10 @@ class PushNotification
* PushNotification constructor.
* @param PushServiceInterface $service By default GCM
*/
public function __construct(PushServiceInterface $service = null)
public function __construct($service = null)
{
$this->service = $service ?: new Gcm;
$this->service = !is_null($service) ? $this->service_collection[$service] : new Fcm;
var_dump($this->service);
}

/**
Expand Down
33 changes: 26 additions & 7 deletions tests/PushNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public function assert_send_method_returns_an_stdClass_instance()
$push = new PushNotification();

$push->setMessage(['message'=>'Hello World'])
->setApiKey('XXofYyQx2SJbumNrs_hUS6Rkrv3W8asd')
->setDevicesToken(['d1WaXouhHG34:AaPA91bF2byCOq-gexmHFqdysYX'])
->setApiKey('AIzaSyAjsu5asdf4N9KyCxCB04')
->setDevicesToken(['howoPaqCPp1pvVsBZ6QUHoEtO_S9-Esel4N7nqeUypQ6ah8MKZKo6jl'])
->setConfig(['dry_run' => true]);

$this->assertInstanceOf('stdClass',$push->send());
$response = $push->send();

$this->assertInstanceOf('stdClass',$response);

}
/** @test */
Expand Down Expand Up @@ -63,7 +65,7 @@ public function set_and_get_service_config(){
$this->assertInternalType('array',$push->config);

/** APNS */
$pushAPN = new PushNotification(new \Edujugon\PushNotification\Apn());
$pushAPN = new PushNotification('apn');

$pushAPN->setConfig(['time_to_live' => 3]);

Expand All @@ -87,7 +89,7 @@ public function set_message_data(){

/** @test */
public function send_method_in_apn_service(){
$push = new PushNotification(new \Edujugon\PushNotification\Apn());
$push = new PushNotification('apn');

$message = [
'aps' => [
Expand Down Expand Up @@ -120,7 +122,7 @@ public function send_method_in_apn_service(){
/** @test */
public function apn_without_certificate()
{
$push = new PushNotification(new \Edujugon\PushNotification\Apn());
$push = new PushNotification('apn');

$push->setConfig(['custom' => 'Custom Value','certificate' => 'MycustomValue']);
$push->send();
Expand All @@ -131,12 +133,29 @@ public function apn_without_certificate()
/** @test */
public function apn_dry_run_option_update_the_apn_url()
{
$push = new PushNotification(new \Edujugon\PushNotification\Apn());
$push = new PushNotification('apn');

$this->assertEquals('ssl://gateway.push.apple.com:2195',$push->url);

$push->setConfig(['dry_run'=>true]);

$this->assertEquals('ssl://gateway.sandbox.push.apple.com:2195',$push->url);
}

/** @test */
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'])
->setConfig(['dry_run' => false]);

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

}
}

0 comments on commit 9985ef4

Please sign in to comment.