-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notification.php
240 lines (207 loc) · 5.77 KB
/
Notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* Unified push notification services abstraction (http://github.com/juliangut/tify).
*
* @license BSD-3-Clause
* @link https://github.com/juliangut/tify
* @author Julián Gutiérrez <juliangut@gmail.com>
*/
namespace Jgut\Tify;
use Jgut\Tify\Receiver\Receiver;
/**
* Notification handler.
*/
class Notification
{
use ParameterTrait {
ParameterTrait::setParameter as innerSetParameter;
}
const PARAMETER_TTL = 'ttl';
// APNS specific
const PARAMETER_BADGE = 'badge';
const PARAMETER_SOUND = 'sound';
const PARAMETER_CONTENT_AVAILABLE = 'content-available';
const PARAMETER_CATEGORY = 'category';
const PARAMETER_URL_ARGS = 'url-args';
// FCM specific
const PARAMETER_PRIORITY = 'priority';
const PARAMETER_COLLAPSE_KEY = 'collapse_key';
const PARAMETER_RESTRICTED_PACKAGE_NAME = 'restricted_package_name';
const PARAMETER_DRY_RUN = 'dry_run';
/**
* Deprecation effective Nov 15th 2016.
* Only kept because of BC. It's ignored by FCM
*
* @deprecated
*/
const PARAMETER_DELAY_WHILE_IDLE = 'delay_while_idle';
const TTL_IMMEDIATE = 300; // 5 minutes
const TTL_FLASH = 3600; // 1 hour
const TTL_SHORT = 86400; // 1 day
const TTL_NORMAL = 604800; // 1 week
const TTL_EXTENDED = 1209600; // 2 week
const TTL_LONG = 1814400; // 3 week
const TTL_EXTRA_LONG = 2419200; // 4 weeks
/**
* Supported parameters.
*
* @var array
*/
protected $supportedParameters = [
self::PARAMETER_TTL,
self::PARAMETER_BADGE,
self::PARAMETER_SOUND,
self::PARAMETER_CONTENT_AVAILABLE,
self::PARAMETER_CATEGORY,
self::PARAMETER_URL_ARGS,
self::PARAMETER_PRIORITY,
self::PARAMETER_COLLAPSE_KEY,
self::PARAMETER_DELAY_WHILE_IDLE,
self::PARAMETER_RESTRICTED_PACKAGE_NAME,
self::PARAMETER_DRY_RUN,
];
/**
* Default notification parameters.
*
* For an iOS silent notification leave "badge", "sound" and "content-available" to null.
*
* @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG
* /Chapters/TheNotificationPayload.html
* @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json
*
* @var array
*/
protected $defaultParameters = [
self::PARAMETER_TTL => self::TTL_EXTENDED,
self::PARAMETER_BADGE => null,
self::PARAMETER_SOUND => null,
self::PARAMETER_CONTENT_AVAILABLE => null,
self::PARAMETER_CATEGORY => null,
self::PARAMETER_URL_ARGS => null,
self::PARAMETER_PRIORITY => 'normal',
self::PARAMETER_COLLAPSE_KEY => null,
self::PARAMETER_RESTRICTED_PACKAGE_NAME => null,
self::PARAMETER_DRY_RUN => false, // It's better to use "sandbox" at adapter level
];
/**
* Notification's message.
*
* @var Message
*/
protected $message;
/**
* Notification list of receivers.
*
* @var Receiver[]
*/
protected $receivers = [];
/**
* Notification constructor.
*
* @param Message $message
* @param Receiver|Receiver[] $receivers
* @param array $parameters
*
* @throws \InvalidArgumentException
*/
public function __construct(Message $message, $receivers = null, array $parameters = [])
{
$this->parameterAliasMap = [
'expire' => static::PARAMETER_TTL,
'time_to_live' => static::PARAMETER_TTL,
];
$this->setParameters(array_merge($this->defaultParameters, $parameters));
$this->message = $message;
if ($receivers !== null) {
if (!is_array($receivers)) {
$receivers = [$receivers];
}
$this->setReceivers($receivers);
}
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException
*/
public function setParameter($parameter, $value)
{
$parameter = $this->getMappedParameter($parameter);
if (!in_array($parameter, $this->supportedParameters)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid notification parameter', $parameter));
}
if ($parameter === static::PARAMETER_DELAY_WHILE_IDLE) {
trigger_error('delay_while_idle parameter is deprecated', E_USER_NOTICE);
}
return $this->innerSetParameter($parameter, $value);
}
/**
* Get message.
*
* @return Message
*/
public function getMessage()
{
return $this->message;
}
/**
* Set message.
*
* @param Message $message
*
* @throws \InvalidArgumentException
*
* @return $this
*/
public function setMessage(Message $message)
{
$this->message = $message;
return $this;
}
/**
* Retrieve list of receivers.
*
* @return Receiver[]
*/
public function getReceivers()
{
return $this->receivers;
}
/**
* Register receivers.
*
* @param array $receivers
*
* @return $this
*/
public function setReceivers(array $receivers)
{
$this->receivers = [];
foreach ($receivers as $receiver) {
$this->addReceiver($receiver);
}
return $this;
}
/**
* Add receiver.
*
* @param Receiver $receiver
*
* @return $this
*/
public function addReceiver(Receiver $receiver)
{
$this->receivers[] = $receiver;
return $this;
}
/**
* Remove all receivers.
*
* @return $this
*/
public function clearReceivers()
{
$this->receivers = [];
return $this;
}
}