19
19
20
20
class WebPush
21
21
{
22
+ const GCM_URL = 'https://android.googleapis.com/gcm/send ' ;
23
+ const TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm ' ;
24
+
22
25
/** @var Browser */
23
26
protected $ browser ;
24
27
@@ -30,7 +33,7 @@ class WebPush
30
33
31
34
/** @var array Array of not standard endpoint sources */
32
35
private $ urlByServerType = array (
33
- 'GCM ' => ' https://android.googleapis.com/gcm/send ' ,
36
+ 'GCM ' => self :: GCM_URL ,
34
37
);
35
38
36
39
/** @var int Time To Live of notifications */
@@ -104,7 +107,7 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
104
107
105
108
// if there has been a problem with at least one notification
106
109
if (is_array ($ res )) {
107
- // if there was only one notification, return the informations directly
110
+ // if there was only one notification, return the information directly
108
111
if (count ($ res ) === 1 ) {
109
112
return $ res [0 ];
110
113
}
@@ -143,14 +146,7 @@ public function flush()
143
146
// for each endpoint server type
144
147
$ responses = array ();
145
148
foreach ($ this ->notificationsByServerType as $ serverType => $ notifications ) {
146
- switch ($ serverType ) {
147
- case 'GCM ' :
148
- $ responses += $ this ->sendToGCMEndpoints ($ notifications );
149
- break ;
150
- case 'standard ' :
151
- $ responses += $ this ->sendToStandardEndpoints ($ notifications );
152
- break ;
153
- }
149
+ $ responses += $ this ->prepareAndSend ($ notifications , $ serverType );
154
150
}
155
151
156
152
// if multi curl, flush
@@ -191,11 +187,12 @@ public function flush()
191
187
return $ completeSuccess ? true : $ return ;
192
188
}
193
189
194
- private function sendToStandardEndpoints (array $ notifications )
190
+ private function prepareAndSend (array $ notifications, $ serverType )
195
191
{
196
192
$ responses = array ();
197
193
/** @var Notification $notification */
198
194
foreach ($ notifications as $ notification ) {
195
+ $ endpoint = $ notification ->getEndpoint ();
199
196
$ payload = $ notification ->getPayload ();
200
197
$ userPublicKey = $ notification ->getUserPublicKey ();
201
198
@@ -205,7 +202,7 @@ private function sendToStandardEndpoints(array $notifications)
205
202
$ headers = array (
206
203
'Content-Length ' => strlen ($ encrypted ['cipherText ' ]),
207
204
'Content-Type ' => 'application/octet-stream ' ,
208
- 'Content-Encoding ' => 'aesgcm128 ' ,
205
+ 'Content-Encoding ' => 'aesgcm ' ,
209
206
'Encryption ' => 'keyid="p256dh";salt=" ' .$ encrypted ['salt ' ].'" ' ,
210
207
'Crypto-Key ' => 'keyid="p256dh";dh=" ' .$ encrypted ['localPublicKey ' ].'" ' ,
211
208
'TTL ' => $ this ->TTL ,
@@ -221,43 +218,14 @@ private function sendToStandardEndpoints(array $notifications)
221
218
$ content = '' ;
222
219
}
223
220
224
- $ responses [] = $ this ->sendRequest ($ notification ->getEndpoint (), $ headers , $ content );
225
- }
226
-
227
- return $ responses ;
228
- }
229
-
230
- private function sendToGCMEndpoints (array $ notifications )
231
- {
232
- $ maxBatchSubscriptionIds = 1000 ;
233
- $ url = $ this ->urlByServerType ['GCM ' ];
234
-
235
- $ headers = array (
236
- 'Authorization ' => 'key= ' .$ this ->apiKeys ['GCM ' ],
237
- 'Content-Type ' => 'application/json ' ,
238
- 'TTL ' => $ this ->TTL ,
239
- );
221
+ if ($ serverType === 'GCM ' ) {
222
+ // FUTURE remove when Chrome servers are all up-to-date
223
+ $ endpoint = str_replace (self ::GCM_URL , self ::TEMP_GCM_URL , $ endpoint );
240
224
241
- $ subscriptionIds = array ();
242
- /** @var Notification $notification */
243
- foreach ($ notifications as $ notification ) {
244
- // get all subscriptions ids
245
- $ endpointsSections = explode ('/ ' , $ notification ->getEndpoint ());
246
- $ subscriptionIds [] = $ endpointsSections [count ($ endpointsSections ) - 1 ];
247
- }
248
-
249
- // chunk by max number
250
- $ batch = array_chunk ($ subscriptionIds , $ maxBatchSubscriptionIds );
251
-
252
- $ responses = array ();
253
- foreach ($ batch as $ subscriptionIds ) {
254
- $ content = json_encode (array (
255
- 'registration_ids ' => $ subscriptionIds ,
256
- ));
257
-
258
- $ headers ['Content-Length ' ] = strlen ($ content );
225
+ $ headers ['Authorization ' ] = 'key= ' .$ this ->apiKeys ['GCM ' ];
226
+ }
259
227
260
- $ responses [] = $ this ->sendRequest ($ url , $ headers , $ content );
228
+ $ responses [] = $ this ->sendRequest ($ endpoint , $ headers , $ content );
261
229
}
262
230
263
231
return $ responses ;
0 commit comments