Skip to content

Commit

Permalink
Fixed tests and Apn response message format
Browse files Browse the repository at this point in the history
  • Loading branch information
sevrugin committed Sep 8, 2021
1 parent ca8f2c1 commit f0ad061
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
49 changes: 45 additions & 4 deletions src/Apn.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ public function getUnregisteredDeviceTokens(array $devices_token)
*/
public function send(array $deviceTokens, array $message)
{
if (false == $this->existCertificate()) {
return $this->feedback;
}

$responseCollection = [
'success' => true,
'errors' => [],
'responses' => [],
'error' => '',
'results' => [],
];

if (!$this->curlMultiHandle) {
Expand Down Expand Up @@ -151,7 +155,9 @@ public function send(array $deviceTokens, array $message)

$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($statusCode === 0) {
$responseCollection['errors'][] = [
$responseCollection['success'] = false;

$responseCollection['error'] = [
'status' => $statusCode,
'headers' => $headers,
'body' => curl_error($handle),
Expand All @@ -162,7 +168,7 @@ public function send(array $deviceTokens, array $message)

$responseCollection['success'] = $responseCollection['success'] && $statusCode == 200;

$responseCollection['responses'][] = [
$responseCollection['results'][] = [
'status' => $statusCode,
'headers' => $headers,
'body' => (string)$body,
Expand Down Expand Up @@ -280,4 +286,39 @@ public function prepareHandle($deviceToken, array $message)
return $ch;
}

/**
* Set the feedback with no exist any certificate.
*
* @return mixed|void
*/
private function messageNoExistCertificate()
{
$response = [
'success' => false,
'error' => "Please, add your APN certificate to the iosCertificates folder." . PHP_EOL
];

$this->setFeedback(json_decode(json_encode($response)));
}

/**
* Check if the certificate file exist.
* @return bool
*/
private function existCertificate()
{
if (isset($this->config['certificate'])) {
$certificate = $this->config['certificate'];
if (!file_exists($certificate)) {
$this->messageNoExistCertificate();
return false;
}

return true;
}

$this->messageNoExistCertificate();
return false;
}

}
6 changes: 3 additions & 3 deletions src/Gcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function setApiKey($apiKey)
*/
public function __construct()
{
$this->url = 'https://android.googleapis.com/gcm/send';
$this->config = $this->initializeConfig('gcm');
$this->url = 'https://fcm.googleapis.com/fcm/send';

$this->config = $this->initializeConfig('fcm');
$this->client = new Client;
}

Expand Down
15 changes: 0 additions & 15 deletions tests/PushNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,6 @@ public function apn_without_certificate()
$this->assertFalse($push->feedback->success);
}

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

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

$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()
{
Expand Down

0 comments on commit f0ad061

Please sign in to comment.