1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Jgut\Tify\Receiver\AbstractReceiver; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Notification handler. |
17
|
|
|
*/ |
18
|
|
|
class Notification |
19
|
|
|
{ |
20
|
|
|
use ParameterTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Default notification parameters. |
24
|
|
|
* |
25
|
|
|
* @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG |
26
|
|
|
* /Chapters/TheNotificationPayload.html |
27
|
|
|
* @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $defaultParameters = [ |
32
|
|
|
// APNS |
33
|
|
|
'badge' => null, |
34
|
|
|
'sound' => null, |
35
|
|
|
'content_available' => null, |
36
|
|
|
'category' => null, |
37
|
|
|
'url-args' => null, |
38
|
|
|
'expire' => null, |
39
|
|
|
|
40
|
|
|
// GCM |
41
|
|
|
'collapse_key' => null, |
42
|
|
|
'delay_while_idle' => null, |
43
|
|
|
'time_to_live' => 2419200, |
44
|
|
|
'restricted_package_name' => null, |
45
|
|
|
'dry_run' => null, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Jgut\Tify\Message |
50
|
|
|
*/ |
51
|
|
|
protected $message; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection |
55
|
|
|
*/ |
56
|
|
|
protected $receivers; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Notification constructor. |
60
|
|
|
* |
61
|
|
|
* @param \Jgut\Tify\Message $message |
62
|
|
|
* @param \Jgut\Tify\Receiver\AbstractReceiver|\Jgut\Tify\Receiver\AbstractReceiver[]| null $receivers |
63
|
|
|
* @param array $parameters |
64
|
|
|
* |
65
|
|
|
* @throws \InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Message $message, $receivers = null, array $parameters = []) |
68
|
|
|
{ |
69
|
|
|
$this->setParameters(array_merge($this->defaultParameters, $parameters)); |
70
|
|
|
|
71
|
|
|
$this->message = $message; |
72
|
|
|
|
73
|
|
|
$this->receivers = new ArrayCollection; |
74
|
|
|
if ($receivers !== null) { |
75
|
|
|
if (!is_array($receivers)) { |
76
|
|
|
$receivers = [$receivers]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->setReceivers($receivers); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get message. |
85
|
|
|
* |
86
|
|
|
* @return \Jgut\Tify\Message |
87
|
|
|
*/ |
88
|
|
|
public function getMessage() |
89
|
|
|
{ |
90
|
|
|
return $this->message; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set message. |
95
|
|
|
* |
96
|
|
|
* @param \Jgut\Tify\Message $message |
97
|
|
|
* |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setMessage(Message $message) |
103
|
|
|
{ |
104
|
|
|
$this->message = $message; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieve list of receivers. |
111
|
|
|
* |
112
|
|
|
* @return \Jgut\Tify\Receiver\AbstractReceiver[] |
113
|
|
|
*/ |
114
|
|
|
public function getReceivers() |
115
|
|
|
{ |
116
|
|
|
return $this->receivers->toArray(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Register receivers. |
121
|
|
|
* |
122
|
|
|
* @param array $receivers |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setReceivers(array $receivers) |
127
|
|
|
{ |
128
|
|
|
$this->receivers->clear(); |
129
|
|
|
|
130
|
|
|
foreach ($receivers as $receiver) { |
131
|
|
|
$this->addReceiver($receiver); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add receiver. |
139
|
|
|
* |
140
|
|
|
* @param \Jgut\Tify\Receiver\AbstractReceiver $receiver |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function addReceiver(AbstractReceiver $receiver) |
145
|
|
|
{ |
146
|
|
|
$this->receivers->add($receiver); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Remove all receivers. |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function clearReceivers() |
157
|
|
|
{ |
158
|
|
|
$this->receivers->clear(); |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|