|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP APNS. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Gennady Telegin <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Apns; |
|
12
|
|
|
|
|
13
|
|
|
use Apns\Exception\ApnsException; |
|
14
|
|
|
use Apns\Handler\GuzzleHandler; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Client. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Client |
|
20
|
|
|
{ |
|
21
|
|
|
const URI_SANDBOX = 'https://api.development.push.apple.com:443'; |
|
22
|
|
|
const URI_PROD = 'https://api.push.apple.com:443'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $useSandbox; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Path to a file containing a private SSL key in PEM format. |
|
31
|
|
|
* If a password is required, then it's an array containing the path to the SSL key in the first array element |
|
32
|
|
|
* followed by the password required for the certificate in the second element. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array|string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $sslCert; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var callable |
|
40
|
|
|
*/ |
|
41
|
|
|
private $handler; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* AppleNotification constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string|array $sslCert string containing certificate file name or array [<filename>,<password>] |
|
47
|
|
|
* @param bool $useSandbox |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($sslCert, $useSandbox = false) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->useSandbox = $useSandbox; |
|
52
|
|
|
$this->sslCert = $sslCert; |
|
53
|
|
|
|
|
54
|
|
|
$this->handler = new GuzzleHandler(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Message $message |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
* |
|
62
|
|
|
* @throws ApnsException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function send(Message $message) |
|
65
|
|
|
{ |
|
66
|
|
|
$handler = $this->handler; |
|
67
|
|
|
|
|
68
|
|
|
return $handler($this, $message); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array|string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getSslCert() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->sslCert; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getServer() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->useSandbox ? self::URI_SANDBOX : self::URI_PROD; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Message $message |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPushURI(Message $message) |
|
93
|
|
|
{ |
|
94
|
|
|
return sprintf('%s/3/device/%s', $this->getServer(), $message->getDeviceIdentifier()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|