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\Adapter\Gcm\GcmMessage; |
14
|
|
|
use Jgut\Tify\Receiver\AbstractReceiver; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Notification handler. |
18
|
|
|
*/ |
19
|
|
|
class Notification |
20
|
|
|
{ |
21
|
|
|
use ParameterTrait { |
22
|
|
|
ParameterTrait::setParameter as setDefinedParameter; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Default notification parameters. |
27
|
|
|
* |
28
|
|
|
* For an iOS silent notification leave "badge" and "sound" to null and set "content-available" to true |
29
|
|
|
* |
30
|
|
|
* @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG |
31
|
|
|
* /Chapters/TheNotificationPayload.html |
32
|
|
|
* @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $defaultParameters = [ |
37
|
|
|
// APNS |
38
|
|
|
'badge' => null, |
39
|
|
|
'sound' => null, |
40
|
|
|
'content-available' => true, // Awake inactive apps in background |
41
|
|
|
'category' => null, |
42
|
|
|
'url-args' => null, |
43
|
|
|
'expire' => null, |
44
|
|
|
|
45
|
|
|
// GCM |
46
|
|
|
'collapse_key' => null, |
47
|
|
|
'delay_while_idle' => null, |
48
|
|
|
'time_to_live' => GcmMessage::DEFAULT_TTL, // 4 weeks |
49
|
|
|
'restricted_package_name' => null, |
50
|
|
|
'dry_run' => null, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Jgut\Tify\Message |
55
|
|
|
*/ |
56
|
|
|
protected $message; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \Doctrine\Common\Collections\ArrayCollection |
60
|
|
|
*/ |
61
|
|
|
protected $receivers; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Notification constructor. |
65
|
|
|
* |
66
|
|
|
* @param \Jgut\Tify\Message $message |
67
|
|
|
* @param \Jgut\Tify\Receiver\AbstractReceiver|\Jgut\Tify\Receiver\AbstractReceiver[]| null $receivers |
68
|
|
|
* @param array $parameters |
69
|
|
|
* |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
*/ |
72
|
|
|
public function __construct(Message $message, $receivers = null, array $parameters = []) |
73
|
|
|
{ |
74
|
|
|
$this->setParameters(array_merge($this->defaultParameters, $parameters)); |
75
|
|
|
|
76
|
|
|
$this->message = $message; |
77
|
|
|
|
78
|
|
|
$this->receivers = new ArrayCollection; |
79
|
|
|
if ($receivers !== null) { |
80
|
|
|
if (!is_array($receivers)) { |
81
|
|
|
$receivers = [$receivers]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->setReceivers($receivers); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
public function setParameter($parameter, $value) |
94
|
|
|
{ |
95
|
|
|
if (!array_key_exists($parameter, $this->defaultParameters)) { |
96
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid notification parameter', $parameter)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->setDefinedParameter($parameter, $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get message. |
104
|
|
|
* |
105
|
|
|
* @return \Jgut\Tify\Message |
106
|
|
|
*/ |
107
|
|
|
public function getMessage() |
108
|
|
|
{ |
109
|
|
|
return $this->message; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set message. |
114
|
|
|
* |
115
|
|
|
* @param \Jgut\Tify\Message $message |
116
|
|
|
* |
117
|
|
|
* @throws \InvalidArgumentException |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setMessage(Message $message) |
122
|
|
|
{ |
123
|
|
|
$this->message = $message; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieve list of receivers. |
130
|
|
|
* |
131
|
|
|
* @return \Jgut\Tify\Receiver\AbstractReceiver[] |
132
|
|
|
*/ |
133
|
|
|
public function getReceivers() |
134
|
|
|
{ |
135
|
|
|
return $this->receivers->toArray(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Register receivers. |
140
|
|
|
* |
141
|
|
|
* @param array $receivers |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setReceivers(array $receivers) |
146
|
|
|
{ |
147
|
|
|
$this->receivers->clear(); |
148
|
|
|
|
149
|
|
|
foreach ($receivers as $receiver) { |
150
|
|
|
$this->addReceiver($receiver); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add receiver. |
158
|
|
|
* |
159
|
|
|
* @param \Jgut\Tify\Receiver\AbstractReceiver $receiver |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function addReceiver(AbstractReceiver $receiver) |
164
|
|
|
{ |
165
|
|
|
$this->receivers->add($receiver); |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Remove all receivers. |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function clearReceivers() |
176
|
|
|
{ |
177
|
|
|
$this->receivers->clear(); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|