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\HandlerFactory; |
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
|
|
|
* @param callable $handler |
49
|
|
|
*/ |
50
|
6 |
|
public function __construct($sslCert, $useSandbox = false, callable $handler = null) |
51
|
|
|
{ |
52
|
6 |
|
$this->useSandbox = $useSandbox; |
53
|
6 |
|
$this->sslCert = $sslCert; |
54
|
|
|
|
55
|
6 |
|
$this->handler = $handler ?: HandlerFactory::create(); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Message $message |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
* |
63
|
|
|
* @throws ApnsException |
64
|
|
|
*/ |
65
|
1 |
|
public function send(Message $message) |
66
|
|
|
{ |
67
|
1 |
|
$handler = $this->handler; |
68
|
|
|
|
69
|
1 |
|
return $handler($this, $message); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array|string |
74
|
|
|
*/ |
75
|
2 |
|
public function getSslCert() |
76
|
|
|
{ |
77
|
2 |
|
return $this->sslCert; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
3 |
|
public function getServer() |
84
|
|
|
{ |
85
|
3 |
|
return $this->useSandbox ? self::URI_SANDBOX : self::URI_PROD; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Message $message |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
1 |
|
public function getPushURI(Message $message) |
94
|
|
|
{ |
95
|
1 |
|
return sprintf('%s/3/device/%s', $this->getServer(), $message->getDeviceIdentifier()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|