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\Adapter\Gcm; |
11
|
|
|
|
12
|
|
|
use Zend\Json\Json; |
13
|
|
|
use ZendService\Google\Exception\InvalidArgumentException; |
14
|
|
|
use ZendService\Google\Exception\RuntimeException; |
15
|
|
|
use ZendService\Google\Gcm\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Custom GCM service message. |
19
|
|
|
* |
20
|
|
|
* Implements notification payload parameters. |
21
|
|
|
*/ |
22
|
|
|
class GcmMessage extends Message |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $notificationPayload = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get notification payload data. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getNotificationPayload() |
35
|
|
|
{ |
36
|
|
|
return $this->notificationPayload; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set notification payload data. |
41
|
|
|
* |
42
|
|
|
* @param array $payload |
43
|
|
|
* |
44
|
|
|
* @throws \ZendService\Google\Exception\InvalidArgumentException |
45
|
|
|
* @throws \ZendService\Google\Exception\RuntimeException |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setNotificationPayload(array $payload) |
50
|
|
|
{ |
51
|
|
|
$this->clearNotificationPayload(); |
52
|
|
|
|
53
|
|
|
foreach ($payload as $key => $value) { |
54
|
|
|
$this->addNotificationPayload($key, $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add notification payload data. |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* @param mixed $value |
65
|
|
|
* |
66
|
|
|
* @throws \ZendService\Google\Exception\InvalidArgumentException |
67
|
|
|
* @throws \ZendService\Google\Exception\RuntimeException |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function addNotificationPayload($key, $value) |
72
|
|
|
{ |
73
|
|
|
$key = trim($key); |
74
|
|
|
|
75
|
|
|
if ($key === '') { |
76
|
|
|
throw new InvalidArgumentException('Notification payload key must be a non-empty string'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (array_key_exists($key, $this->notificationPayload)) { |
80
|
|
|
throw new RuntimeException(sprintf('"%s" conflicts with current set notification payload data', $key)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->notificationPayload[$key] = $value; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Clear notification payload data. |
90
|
|
|
*/ |
91
|
|
|
public function clearNotificationPayload() |
92
|
|
|
{ |
93
|
|
|
$this->notificationPayload = []; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function toJson() |
102
|
|
|
{ |
103
|
|
|
$json = []; |
104
|
|
|
|
105
|
|
|
if (count($this->registrationIds)) { |
106
|
|
|
$json['registration_ids'] = $this->registrationIds; |
107
|
|
|
} |
108
|
|
|
if ($this->collapseKey) { |
109
|
|
|
$json['collapse_key'] = $this->collapseKey; |
110
|
|
|
} |
111
|
|
|
if ($this->delayWhileIdle) { |
112
|
|
|
$json['delay_while_idle'] = $this->delayWhileIdle; |
113
|
|
|
} |
114
|
|
|
if ($this->timeToLive !== 2419200) { |
115
|
|
|
$json['time_to_live'] = $this->timeToLive; |
116
|
|
|
} |
117
|
|
|
if ($this->restrictedPackageName) { |
118
|
|
|
$json['restricted_package_name'] = $this->restrictedPackageName; |
119
|
|
|
} |
120
|
|
|
if ($this->dryRun) { |
121
|
|
|
$json['dry_run'] = $this->dryRun; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$json = array_merge($json, $this->getCompoundPayload()); |
125
|
|
|
|
126
|
|
|
return Json::encode($json); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve payload. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function getCompoundPayload() |
135
|
|
|
{ |
136
|
|
|
$payload = []; |
137
|
|
|
|
138
|
|
|
if (count($this->data)) { |
139
|
|
|
$payload['data'] = $this->data; |
140
|
|
|
} |
141
|
|
|
if (count($this->notificationPayload)) { |
142
|
|
|
$payload['notification'] = $this->notificationPayload; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $payload; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|