Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit f948adb

Browse files
committed
Merge pull request #2 from jwage/feature/max-payload-bytes
Implement payload max bytes check.
2 parents 45abd9f + 69af900 commit f948adb

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/JWage/APNS/SocketClient.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class SocketClient
88
{
9+
const PAYLOAD_MAX_BYTES = 256;
10+
911
/**
1012
* @var \JWage\APNS\Certificate
1113
*/
@@ -65,6 +67,12 @@ public function __destruct()
6567
*/
6668
public function write($binaryMessage)
6769
{
70+
if (strlen($binaryMessage) > self::PAYLOAD_MAX_BYTES) {
71+
throw new \InvalidArgumentException(
72+
sprintf('The maximum size allowed for a notification payload is %s bytes; Apple Push Notification Service refuses any notification that exceeds this limit.', self::PAYLOAD_MAX_BYTES)
73+
);
74+
}
75+
6876
return fwrite($this->getApnsResource(), $binaryMessage);
6977
}
7078

tests/JWage/APNS/Tests/SocketClientTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,44 @@ public function testWrite()
2626
$this->assertEquals('test', file_get_contents($this->testPath));
2727
}
2828

29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit.
32+
*/
33+
public function testWriteMaxBytes()
34+
{
35+
$deviceToken = '97213C2CA2146AF258B098611394FD6943FA730FF65E6797A85D3A0DC713A84C';
36+
37+
$payload = array(
38+
'aps' => array(
39+
'alert' => array(
40+
'title' => 'Website',
41+
'body' => 'Jonathan H. Wage joined your website. This is a really long push notification body. So long it probably won\'t work! Not long enough! Perfect!',
42+
),
43+
'url-args' => array(
44+
'http://google.com',
45+
),
46+
),
47+
);
48+
49+
$encodedPayload = json_encode($payload);
50+
51+
$payload = chr(0).
52+
chr(0).
53+
chr(32).
54+
pack('H*', $deviceToken).
55+
chr(0).chr(strlen($encodedPayload)).
56+
$encodedPayload;
57+
58+
// write binary string to file
59+
$path = sprintf('%s/php_apns_test_write_max_bytes', sys_get_temp_dir());
60+
file_put_contents($path, $payload);
61+
62+
$this->assertEquals(filesize($path), strlen($payload), 'Compare result of filesize() to strlen()');
63+
64+
$this->socketClient->write($payload);
65+
}
66+
2967
public function testCreateStreamContext()
3068
{
3169
$streamContext = $this->socketClient->getTestCreateStreamContext();

0 commit comments

Comments
 (0)