Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9d5e9d

Browse files
committed
APNsConfig Implemented & TTL Interval Parameter Changed
* Implemented apns-collapse-id, apns-expiration, apns-priority * CommonConfig accepts setTimeToLive as $time * AndroidConfig will accept it as seconds, while APNsConfig accepts it as days. * Intergrated configuration class will accept it as days, and convert it to each config's option (seconds for Android, UNIX epoch time to APNs)
1 parent 85835f1 commit e9d5e9d

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

‎src/Config/APNsConfig.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lkaybob
5+
* Date: 31/03/2018
6+
* Time: 00:12
7+
*/
8+
9+
namespace phpFCMv1\Config;
10+
11+
class APNsConfig implements CommonConfig {
12+
const PRIORITY_HIGH = 10, PRIORITY_NORMAL = 5;
13+
private $payload;
14+
15+
public function __construct() {
16+
$this -> payload = array();
17+
}
18+
19+
/**
20+
* @param $key
21+
* @return mixed
22+
*/
23+
function setCollapseKey($key) {
24+
$payload = array_merge($this -> payload, array('apns-collapse-id' => $key));
25+
$this -> payload = $payload;
26+
27+
return null;
28+
}
29+
30+
/**
31+
* @param $priority
32+
* @return mixed
33+
*/
34+
function setPriority($priority) {
35+
$payload = array_merge($this -> payload, array('apns-priority' => $priority));
36+
$this -> payload = $payload;
37+
38+
return null;
39+
}
40+
41+
/**
42+
* @param $time : Time for notification to live in days
43+
* @return mixed : Expiration option using UNIX epoch date
44+
* @throws \Exception
45+
*/
46+
function setTimeToLive($time) {
47+
$expiration = new \DateTime('now');
48+
$expiration -> add(new \DateInterval('P' . $time . 'D'));
49+
$expValue = $expiration -> format('U');
50+
51+
$payload = array_merge($this -> payload, array('apns-expiration' => $expValue));
52+
$this -> payload = $payload;
53+
54+
return null;
55+
}
56+
57+
/**
58+
* @return mixed
59+
*/
60+
public function getPayload() {
61+
if (!sizeof($this -> payload)) {
62+
return null;
63+
} else {
64+
$payload = array('apns' => $this -> payload);
65+
return $payload;
66+
}
67+
}
68+
}

‎src/Config/AndroidConfig.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ function setPriority($priority) {
3939
}
4040

4141
/**
42-
* @param $seconds
42+
* @param $time
4343
* @return mixed
4444
*/
45-
function setTimeToLive($seconds) {
46-
$payload = array_merge($this -> payload, array('ttl' => $seconds . 's'));
45+
function setTimeToLive($time) {
46+
$payload = array_merge($this -> payload, array('ttl' => $time . 's'));
4747
$this -> payload = $payload;
4848

4949
return null;

‎src/Config/CommonConfig.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function setCollapseKey($key);
2424
function setPriority($priority);
2525

2626
/**
27-
* @param $seconds
27+
* @param $time
2828
* @return mixed
2929
*/
30-
function setTimeToLive($seconds);
30+
function setTimeToLive($time);
3131

3232
/**
3333
* @return mixed

‎tests/APNsConfigTest.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: lkaybob
5+
* Date: 05/04/2018
6+
* Time: 21:05
7+
*/
8+
9+
namespace phpFCMv1\tests;
10+
11+
use phpFCMv1\Config\APNsConfig;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class APNsConfigTest extends TestCase {
15+
public function testSetCollapseKey() {
16+
$instance = new APNsConfig();
17+
$instance -> setCollapseKey('Test');
18+
$payload = $instance -> getPayload();
19+
20+
$this -> assertArrayHasKey('apns', $payload);
21+
$this -> assertArrayHasKey('apns-collapse-id', $payload['apns']);
22+
}
23+
24+
public function testSetPriority() {
25+
$instance = new APNsConfig();
26+
$instance -> setPriority(APNsConfig::PRIORITY_HIGH);
27+
$payload = $instance -> getPayload();
28+
29+
$this -> assertArrayHasKey('apns', $payload);
30+
$this -> assertArrayHasKey('apns-priority', $payload['apns']);
31+
}
32+
33+
public function testSetTimeToLive() {
34+
$start = new \DateTime('now');
35+
36+
$instance = new APNsConfig();
37+
try {
38+
$instance -> setTimeToLive(1);
39+
} catch (\Exception $e) {
40+
}
41+
$payload = $instance -> getPayload();
42+
43+
$this -> assertArrayHasKey('apns', $payload);
44+
$this -> assertArrayHasKey('apns-expiration', $payload['apns']);
45+
46+
$end = new \DateTime('@'.$payload['apns']['apns-expiration']);
47+
$this -> assertEquals(1, $end -> diff($start) -> d);
48+
}
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /