1919
2020class WebPush
2121{
22+ const GCM_URL = 'https://android.googleapis.com/gcm/send ' ;
23+ const TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm ' ;
24+
2225 /** @var Browser */
2326 protected $ browser ;
2427
@@ -30,7 +33,7 @@ class WebPush
3033
3134 /** @var array Array of not standard endpoint sources */
3235 private $ urlByServerType = array (
33- 'GCM ' => ' https://android.googleapis.com/gcm/send ' ,
36+ 'GCM ' => self :: GCM_URL ,
3437 );
3538
3639 /** @var int Time To Live of notifications */
@@ -104,7 +107,7 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
104107
105108 // if there has been a problem with at least one notification
106109 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
108111 if (count ($ res ) === 1 ) {
109112 return $ res [0 ];
110113 }
@@ -143,14 +146,7 @@ public function flush()
143146 // for each endpoint server type
144147 $ responses = array ();
145148 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 );
154150 }
155151
156152 // if multi curl, flush
@@ -191,11 +187,12 @@ public function flush()
191187 return $ completeSuccess ? true : $ return ;
192188 }
193189
194- private function sendToStandardEndpoints (array $ notifications )
190+ private function prepareAndSend (array $ notifications, $ serverType )
195191 {
196192 $ responses = array ();
197193 /** @var Notification $notification */
198194 foreach ($ notifications as $ notification ) {
195+ $ endpoint = $ notification ->getEndpoint ();
199196 $ payload = $ notification ->getPayload ();
200197 $ userPublicKey = $ notification ->getUserPublicKey ();
201198
@@ -205,7 +202,7 @@ private function sendToStandardEndpoints(array $notifications)
205202 $ headers = array (
206203 'Content-Length ' => strlen ($ encrypted ['cipherText ' ]),
207204 'Content-Type ' => 'application/octet-stream ' ,
208- 'Content-Encoding ' => 'aesgcm128 ' ,
205+ 'Content-Encoding ' => 'aesgcm ' ,
209206 'Encryption ' => 'keyid="p256dh";salt=" ' .$ encrypted ['salt ' ].'" ' ,
210207 'Crypto-Key ' => 'keyid="p256dh";dh=" ' .$ encrypted ['localPublicKey ' ].'" ' ,
211208 'TTL ' => $ this ->TTL ,
@@ -221,43 +218,14 @@ private function sendToStandardEndpoints(array $notifications)
221218 $ content = '' ;
222219 }
223220
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 );
240224
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+ }
259227
260- $ responses [] = $ this ->sendRequest ($ url , $ headers , $ content );
228+ $ responses [] = $ this ->sendRequest ($ endpoint , $ headers , $ content );
261229 }
262230
263231 return $ responses ;
0 commit comments