Skip to content

Commit

Permalink
Fix apns-id not being sent to Apple
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasa Blagojevic authored and Sasa Blagojevic committed Sep 1, 2020
1 parent a02aa1a commit 994ae5d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
6 changes: 6 additions & 0 deletions example/certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SasaB\Apns\Provider\Certificate;
use SasaB\Apns\Client;
use SasaB\Apns\Notification;
use \Ramsey\Uuid\Uuid;

$certificate = Certificate::fromFile('./PushCert.pem');

Expand All @@ -19,8 +20,13 @@
// 51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb
$notification = new Notification("51d5f3696c9cc62caf322fbcfd0b25a455697b1c3261eb4ed085041c6e895bdb");

$notification->setApsId($apsId = Uuid::uuid4());

$notification->setCustomKey('mdm', ['PushMagic' => '4DA9FEC7-5443-48B3-9491-892F1147BE47']);


$response = $client->send($notification);

echo (string) $apsId."\n";
echo (string) $response->getApnsId()."\n";
echo $response;
3 changes: 2 additions & 1 deletion src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function getPushType()
public function getHeaders(): array
{
$headers = [
Header::APNS_ID => (string) $this->apsId,
Header::APNS_EXPIRATION => $this->expiresAt,
Header::APNS_TOPIC => $this->pushTopic,
Header::APNS_COLLAPSE_ID => $this->collapseId,
Expand All @@ -200,7 +201,7 @@ public function getHeaders(): array
];

foreach ($headers as $k => $v) {
if ($v === null) unset($headers[$k]);
if (!$v) unset($headers[$k]);
}

return $headers;
Expand Down
2 changes: 1 addition & 1 deletion src/Payload/Aps.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function setAlert(Alert $alert): Aps
* @param mixed $badge
* @return Aps
*/
public function setBadge(string $badge): Aps
public function setBadge(int $badge): Aps
{
$this->badge = $badge;
return $this;
Expand Down
1 change: 0 additions & 1 deletion src/Provider/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function getAuthOptions(): array
{
return [
'headers' => [
Header::APNS_TOPIC => '',
Header::AUTHORIZATION => "Bearer: $this"
]
];
Expand Down
6 changes: 3 additions & 3 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function testItCanSetCustomPayload()

$payload = $message->getCustom();

$this->assertEquals($payload, ['acme' => 'baz', 'foo' => 'bar']);
$this->assertEquals(['acme' => 'baz', 'foo' => 'bar'], $payload);
$this->assertSame(json_encode($payload), (string) $message);
}

public function testItCanSetMdmPayload()
{
$mdmPayload = ['mdm' => ['PushMagic' => 'xxx-xxx-xxx-xxx']];
$mdmPayload = ['mdm' => 'xxx-xxx-xxx-xxx'];

$message = new Notification('device-token');

$message->setCustomKey('mdm', ['PushMagic' => 'xxx-xxx-xxx-xxx']);
$message->setCustomKey('mdm', 'xxx-xxx-xxx-xxx');

$this->assertInstanceOf(UuidInterface::class, $message->getApsId());
$this->assertSame($mdmPayload, $message->getCustom());
Expand Down
73 changes: 72 additions & 1 deletion tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,78 @@
namespace SasaB\Tests;


class PayloadTest
use PHPUnit\Framework\TestCase;
use SasaB\Apns\Payload\Alert;
use SasaB\Apns\Payload\Aps;

class PayloadTest extends TestCase
{
public function testAlert()
{
$alert = new Alert('');

$data = [
'title' => 'Test Title',
'body' => 'Test Body',
'title-loc-key' => 'Title Localization Key',
'title-loc-args' => ['These', 'are', 'all', 'test', 'values'],
'action-loc-key' => 'Test Action Localization Key',
'loc-key' => 'Localization Key',
'loc-args' => ['These', 'are', 'all', 'localization', 'values'],
'launch-image' => base64_encode(random_bytes(36))
];

$alert->setTitle($data['title']);
$alert->setText($data['body']);
$alert->setTitleLocKey($data['title-loc-key']);
$alert->setTitleLocArgs($data['title-loc-args']);
$alert->setActionLocKey($data['action-loc-key']);
$alert->setLocKey($data['loc-key']);
$alert->setLocArgs($data['loc-args']);
$alert->setLaunchImage($data['launch-image']);

$this->assertSame($data, $alert->jsonSerialize());
$this->assertSame(json_encode($data), json_encode($alert));
}

public function testApsWithAlert()
{
$alertData = [
'title' => 'Test Title',
'body' => 'Test Body',
'title-loc-key' => 'Title Localization Key',
'title-loc-args' => ['These', 'are', 'all', 'test', 'values'],
'action-loc-key' => 'Test Action Localization Key',
'loc-key' => 'Localization Key',
'loc-args' => ['These', 'are', 'all', 'localization', 'values'],
'launch-image' => base64_encode(random_bytes(36))
];

$alert = new Alert('Test Body');
$alert->setTitle($alertData['title']);
$alert->setTitleLocKey($alertData['title-loc-key']);
$alert->setTitleLocArgs($alertData['title-loc-args']);
$alert->setActionLocKey($alertData['action-loc-key']);
$alert->setLocKey($alertData['loc-key']);
$alert->setLocArgs($alertData['loc-args']);
$alert->setLaunchImage($alertData['launch-image']);

$apsData = [
'alert' => $alertData,
'badge' => 1,
'sound' => 'alarm.aiff',
'content-available' => 1,
'category' => 'general',
'thread-id' => md5('hi')
];

$aps = new Aps($alert);
$aps->setBadge($apsData['badge']);
$aps->setSound($apsData['sound']);
$aps->setContentAvailable($apsData['content-available']);
$aps->setCategory($apsData['category']);
$aps->setThreadId($apsData['thread-id']);

$this->assertSame(array_merge($apsData, ['alert' => $alertData]), $aps->asArray());
}
}

0 comments on commit 994ae5d

Please sign in to comment.