@@ -25,6 +25,14 @@ class WebPush
25
25
/** @var array Key is push server type and value is the API key */
26
26
protected $ apiKeys ;
27
27
28
+ /** @var array Array of array of Notifications by server type */
29
+ private $ notificationsByServerType ;
30
+
31
+ /** @var array Array of not standard endpoint sources */
32
+ private $ urlByServerType = array (
33
+ 'GCM ' => 'https://android.googleapis.com/gcm/send ' ,
34
+ );
35
+
28
36
/**
29
37
* WebPush constructor.
30
38
*
@@ -45,57 +53,56 @@ public function __construct(array $apiKeys = array(), $TTL = null, $timeout = nu
45
53
}
46
54
47
55
/**
48
- * Send one notification.
56
+ * Send a notification.
49
57
*
50
58
* @param string $endpoint
51
- * @param string|null $payload If you want to send an array, json_encode it.
59
+ * @param string|null $payload If you want to send an array, json_encode it.
52
60
* @param string|null $userPublicKey
61
+ * @param bool $flush If you want to flush directly (usually when you send only one notification)
53
62
*
54
- * @return array
55
- *
63
+ * @return bool|array Return an array of information if $flush is set to true and the request has failed. Else return true.
56
64
* @throws \ErrorException
57
65
*/
58
- public function sendNotification ($ endpoint , $ payload = null , $ userPublicKey = null )
66
+ public function sendNotification ($ endpoint , $ payload = null , $ userPublicKey = null , $ flush = false )
59
67
{
60
- $ endpoints = array ( $ endpoint );
61
- $ payloads = isset ( $ payload ) ? array ( $ payload ) : null ;
62
- $ userPublicKeys = isset ( $ userPublicKey ) ? array ( $ userPublicKey ) : null ;
68
+ // sort notification by server type
69
+ $ type = $ this -> sortEndpoint ( $ endpoint ) ;
70
+ $ this -> notificationsByServerType [ $ type ][] = new Notification ( $ endpoint , $ payload , $ userPublicKey );
63
71
64
- return $ this ->sendNotifications ($ endpoints , $ payloads , $ userPublicKeys );
72
+ if ($ flush ) {
73
+ $ res = $ this ->flush ();
74
+ return is_array ($ res ) ? $ res [0 ] : true ;
75
+ }
76
+
77
+ return true ;
65
78
}
66
79
67
80
/**
68
- * Send multiple notifications.
69
- *
70
- * @param array $endpoints
71
- * @param array|null $payloads
72
- * @param array|null $userPublicKeys
81
+ * Flush notifications. Triggers the requests.
73
82
*
74
- * @return array
83
+ * @return array|bool If there are no errors, return true.
84
+ * Else return an array of information for each notification sent (success, statusCode, headers).
75
85
*
76
86
* @throws \ErrorException
77
87
*/
78
- public function sendNotifications ( array $ endpoints , array $ payloads = null , array $ userPublicKeys = null )
88
+ public function flush ( )
79
89
{
80
- // sort endpoints by server type
81
- $ endpointsByServerType = $ this ->sortEndpoints ($ endpoints );
82
-
83
- // if GCM we should check for the API key
84
- if (array_key_exists ('GCM ' , $ endpointsByServerType )) {
90
+ // if GCM is present, we should check for the API key
91
+ if (array_key_exists ('GCM ' , $ this ->notificationsByServerType )) {
85
92
if (empty ($ this ->apiKeys ['GCM ' ])) {
86
93
throw new \ErrorException ('No GCM API Key specified. ' );
87
94
}
88
95
}
89
96
90
97
// for each endpoint server type
91
98
$ responses = array ();
92
- foreach ($ endpointsByServerType as $ serverType => $ endpoints ) {
99
+ foreach ($ this -> notificationsByServerType as $ serverType => $ notifications ) {
93
100
switch ($ serverType ) {
94
101
case 'GCM ' :
95
- $ responses += $ this ->sendToGCMEndpoints ($ endpoints );
102
+ $ responses += $ this ->sendToGCMEndpoints ($ notifications );
96
103
break ;
97
104
case 'standard ' :
98
- $ responses += $ this ->sendToStandardEndpoints ($ endpoints , $ payloads , $ userPublicKeys );
105
+ $ responses += $ this ->sendToStandardEndpoints ($ notifications );
99
106
break ;
100
107
}
101
108
}
@@ -108,25 +115,34 @@ public function sendNotifications(array $endpoints, array $payloads = null, arra
108
115
}
109
116
110
117
/** @var Response|null $response */
118
+ $ return = array ();
119
+ $ completeSuccess = true ;
111
120
foreach ($ responses as $ response ) {
112
121
if (!isset ($ response )) {
113
- return array (
122
+ $ return[] = array (
114
123
'success ' => false ,
115
124
);
125
+
126
+ $ completeSuccess = false ;
116
127
} elseif (!$ response ->isSuccessful ()) {
117
- return array (
128
+ $ return[] = array (
118
129
'success ' => false ,
119
130
'statusCode ' => $ response ->getStatusCode (),
120
131
'headers ' => $ response ->getHeaders (),
121
132
);
133
+
134
+ $ completeSuccess = false ;
135
+ } else {
136
+ $ return [] = array (
137
+ 'success ' => true ,
138
+ );
122
139
}
123
140
}
124
141
125
- return array (
126
- 'success ' => true ,
127
- );
142
+ return $ completeSuccess ? true : $ return ;
128
143
}
129
- private function sendToStandardEndpoints (array $ endpoints , array $ payloads = null , array $ userPublicKeys = null )
144
+
145
+ private function sendToStandardEndpoints (array $ notifications )
130
146
{
131
147
$ headers = array (
132
148
'Content-Length ' => 0 ,
@@ -139,30 +155,27 @@ private function sendToStandardEndpoints(array $endpoints, array $payloads = nul
139
155
}
140
156
141
157
$ responses = array ();
142
- foreach ($ endpoints as $ i => $ endpoint ) {
143
- $ responses [] = $ this ->sendRequest ($ endpoint , $ headers , $ content );
158
+ /** @var Notification $notification */
159
+ foreach ($ notifications as $ notification ) {
160
+ $ responses [] = $ this ->sendRequest ($ notification ->getEndpoint (), $ headers , $ content );
144
161
}
145
162
146
163
return $ responses ;
147
164
}
148
165
149
- /**
150
- * @param array $endpoints
151
- *
152
- * @return array
153
- */
154
- private function sendToGCMEndpoints (array $ endpoints )
166
+ private function sendToGCMEndpoints (array $ notifications )
155
167
{
156
168
$ maxBatchSubscriptionIds = 1000 ;
157
- $ url = ' https://android.googleapis.com/gcm/send ' ;
169
+ $ url = $ this -> urlByServerType [ ' GCM ' ] ;
158
170
159
171
$ headers ['Authorization ' ] = 'key= ' .$ this ->apiKeys ['GCM ' ];
160
172
$ headers ['Content-Type ' ] = 'application/json ' ;
161
173
162
174
$ subscriptionIds = array ();
163
- foreach ($ endpoints as $ endpoint ) {
175
+ /** @var Notification $notification */
176
+ foreach ($ notifications as $ notification ) {
164
177
// get all subscriptions ids
165
- $ endpointsSections = explode ('/ ' , $ endpoint );
178
+ $ endpointsSections = explode ('/ ' , $ notification -> getEndpoint () );
166
179
$ subscriptionIds [] = $ endpointsSections [count ($ endpointsSections ) - 1 ];
167
180
}
168
181
@@ -202,35 +215,19 @@ private function sendRequest($url, array $headers, $content)
202
215
}
203
216
204
217
/**
205
- * @param array $endpoints
218
+ * @param string $endpoint
206
219
*
207
- * @return array
220
+ * @return string
208
221
*/
209
- private function sortEndpoints ( array $ endpoints )
222
+ private function sortEndpoint ( $ endpoint )
210
223
{
211
- $ sortedEndpoints = array ();
212
-
213
- $ serverTypesByUrl = array (
214
- 'GCM ' => 'https://android.googleapis.com/gcm/send ' ,
215
- );
216
-
217
- foreach ($ endpoints as $ endpoint ) {
218
- $ standard = true ;
219
-
220
- foreach ($ serverTypesByUrl as $ type => $ url ) {
221
- if (substr ($ endpoint , 0 , strlen ($ url )) === $ url ) {
222
- $ sortedEndpoints [$ type ][] = $ endpoint ;
223
- $ standard = false ;
224
- break ;
225
- }
226
- }
227
-
228
- if ($ standard ) {
229
- $ sortedEndpoints ['standard ' ][] = $ endpoint ;
224
+ foreach ($ this ->urlByServerType as $ type => $ url ) {
225
+ if (substr ($ endpoint , 0 , strlen ($ url )) === $ url ) {
226
+ return $ type ;
230
227
}
231
228
}
232
229
233
- return $ sortedEndpoints ;
230
+ return ' standard ' ;
234
231
}
235
232
236
233
/**
0 commit comments