1
|
|
|
<?php |
2
|
|
|
namespace ker0x\Push\Adapter\Fcm\Message; |
3
|
|
|
|
4
|
|
|
use Cake\Utility\Inflector; |
5
|
|
|
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Notification |
9
|
|
|
* @package ker0x\Push\Adapter\Fcm\Message |
10
|
|
|
*/ |
11
|
|
|
class Notification |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Indicates notification title. |
16
|
|
|
* |
17
|
|
|
* @var null|string |
18
|
|
|
*/ |
19
|
|
|
protected $title; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Indicates notification body text. |
23
|
|
|
* |
24
|
|
|
* @var null|string |
25
|
|
|
*/ |
26
|
|
|
protected $body; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Indicates a sound to play when the device receives a notification. |
30
|
|
|
* |
31
|
|
|
* @var null|string |
32
|
|
|
*/ |
33
|
|
|
protected $sound; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Indicates the badge on the client app home icon. (iOS) |
37
|
|
|
* |
38
|
|
|
* @var null|string |
39
|
|
|
*/ |
40
|
|
|
protected $badge; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Indicates notification icon. (Android) |
44
|
|
|
* |
45
|
|
|
* @var null|string |
46
|
|
|
*/ |
47
|
|
|
protected $icon; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Indicates whether each notification results in a new entry in the |
51
|
|
|
* notification drawer on Android. (Android) |
52
|
|
|
* |
53
|
|
|
* @var null|string |
54
|
|
|
*/ |
55
|
|
|
protected $tag; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Indicates color of the icon, expressed in #rrggbb format. (Android) |
59
|
|
|
* |
60
|
|
|
* @var null|string |
61
|
|
|
*/ |
62
|
|
|
protected $color; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Indicates the action associated with a user click on the notification. |
66
|
|
|
* |
67
|
|
|
* @var null|string |
68
|
|
|
*/ |
69
|
|
|
protected $clickAction; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Indicates the key to the body string for localization. |
73
|
|
|
* |
74
|
|
|
* @var null|string |
75
|
|
|
*/ |
76
|
|
|
protected $bodyLocKey; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Indicates the string value to replace format specifiers in the |
80
|
|
|
* body string for localization. |
81
|
|
|
* |
82
|
|
|
* @var null|string |
83
|
|
|
*/ |
84
|
|
|
protected $bodyLocArgs; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Indicates the key to the title string for localization. |
88
|
|
|
* |
89
|
|
|
* @var null|string |
90
|
|
|
*/ |
91
|
|
|
protected $titleLocKey; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Indicates the string value to replace format specifiers in |
95
|
|
|
* the title string for localization. |
96
|
|
|
* |
97
|
|
|
* @var null|string |
98
|
|
|
*/ |
99
|
|
|
protected $titleLocArgs; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Notification constructor. |
103
|
|
|
* |
104
|
|
|
* @param array|\ker0x\Push\Adapter\Fcm\Message\NotificationBuilder $notificationBuilder The notification we want to send |
105
|
|
|
*/ |
106
|
|
|
public function __construct($notificationBuilder) |
107
|
|
|
{ |
108
|
|
|
if (is_array($notificationBuilder)) { |
109
|
|
|
$notificationBuilder = $this->fromArray($notificationBuilder); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->title = $notificationBuilder->getTitle(); |
113
|
|
|
$this->body = $notificationBuilder->getBody(); |
114
|
|
|
$this->sound = $notificationBuilder->getSound(); |
115
|
|
|
$this->badge = $notificationBuilder->getBadge(); |
116
|
|
|
$this->icon = $notificationBuilder->getIcon(); |
117
|
|
|
$this->tag = $notificationBuilder->getTag(); |
118
|
|
|
$this->color = $notificationBuilder->getColor(); |
119
|
|
|
$this->clickAction = $notificationBuilder->getClickAction(); |
120
|
|
|
$this->bodyLocKey = $notificationBuilder->getBodyLocKey(); |
121
|
|
|
$this->bodyLocArgs = $notificationBuilder->getBodyLocArgs(); |
122
|
|
|
$this->titleLocKey = $notificationBuilder->getTitleLocKey(); |
123
|
|
|
$this->titleLocArgs = $notificationBuilder->getTitleLocArgs(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return notification as an array. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function build() |
132
|
|
|
{ |
133
|
|
|
$notification = [ |
134
|
|
|
'title' => $this->title, |
135
|
|
|
'body' => $this->body, |
136
|
|
|
'sound' => $this->sound, |
137
|
|
|
'badge' => $this->badge, |
138
|
|
|
'icon' => $this->icon, |
139
|
|
|
'tag' => $this->tag, |
140
|
|
|
'color' => $this->color, |
141
|
|
|
'click_action' => $this->clickAction, |
142
|
|
|
'body_loc_key' => $this->bodyLocKey, |
143
|
|
|
'body_loc_args' => $this->bodyLocArgs, |
144
|
|
|
'title_loc_key' => $this->titleLocKey, |
145
|
|
|
'title_loc_args' => $this->titleLocArgs, |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
return array_filter($notification); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Build notification from an array |
153
|
|
|
* |
154
|
|
|
* @param array $notificationArray Array of keys for the notification. |
155
|
|
|
* @return \ker0x\Push\Adapter\Fcm\Message\NotificationBuilder |
156
|
|
|
* @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidNotificationException |
157
|
|
|
*/ |
158
|
|
|
private function fromArray(array $notificationArray) |
159
|
|
|
{ |
160
|
|
|
if (empty($notificationArray)) { |
161
|
|
|
throw InvalidNotificationException::arrayEmpty(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!isset($notificationArray['title'])) { |
165
|
|
|
throw InvalidNotificationException::invalidArray(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$notificationBuilder = new NotificationBuilder($notificationArray['title']); |
169
|
|
|
unset($notificationArray['title']); |
170
|
|
|
foreach ($notificationArray as $key => $value) { |
171
|
|
|
$key = Inflector::camelize($key); |
172
|
|
|
$setter = 'set' . $key; |
173
|
|
|
$notificationBuilder->$setter($value); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $notificationBuilder; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|