|
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\Notification; |
|
11
|
|
|
|
|
12
|
|
|
use Jgut\Tify\Service\AbstractService; |
|
13
|
|
|
use Jgut\Tify\Service\GcmService as GcmService; |
|
14
|
|
|
use Jgut\Tify\Recipient\AbstractRecipient; |
|
15
|
|
|
use Jgut\Tify\Recipient\GcmRecipient as GcmRecipient; |
|
16
|
|
|
use Jgut\Tify\Message\AbstractMessage; |
|
17
|
|
|
use Jgut\Tify\Message\GcmMessage as GcmMessage; |
|
18
|
|
|
|
|
19
|
|
|
class GcmNotification extends AbstractNotification |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param \Jgut\Tify\Service\GcmService $service |
|
23
|
|
|
* @param \Jgut\Tify\Message\GcmMessage $message |
|
24
|
|
|
* @param \Jgut\Tify\Recipient\GcmRecipient[] $recipients |
|
25
|
|
|
* @param array $options |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(GcmService $service, GcmMessage $message, array $recipients = [], array $options = []) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($service, $message, $recipients, $options); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getDefaultOptions() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'collapse_key' => null, |
|
39
|
|
|
'delay_while_idle' => null, |
|
40
|
|
|
'time_to_live' => 2419200, |
|
41
|
|
|
'restricted_package_name' => null, |
|
42
|
|
|
'dry_run' => null, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setService(AbstractService $service) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$service instanceof GcmService) { |
|
54
|
|
|
throw new \InvalidArgumentException('Service must be an accepted GCM service'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->service = $service; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \InvalidArgumentException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setMessage(AbstractMessage $message) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$message instanceof GcmMessage) { |
|
70
|
|
|
throw new \InvalidArgumentException('Message must be an accepted GCM message'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->message = $message; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \InvalidArgumentException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function addRecipient(AbstractRecipient $recipient) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$recipient instanceof GcmRecipient) { |
|
86
|
|
|
throw new \InvalidArgumentException('Recipient must be an accepted GCM recipient'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->recipients[$recipient->getToken()] = $recipient; |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|