20
20
class WebPush
21
21
{
22
22
const GCM_URL = 'https://android.googleapis.com/gcm/send ' ;
23
+ const FCM_URL = 'https://fcm.googleapis.com/fcm/send ' ;
23
24
24
25
/** @var Browser */
25
26
protected $ browser ;
26
27
27
- /** @var array Key is push server type and value is the API key */
28
- protected $ apiKeys ;
28
+ /** @var array */
29
+ protected $ auth ;
29
30
30
31
/** @var array Array of array of Notifications */
31
32
private $ notifications ;
@@ -42,14 +43,14 @@ class WebPush
42
43
/**
43
44
* WebPush constructor.
44
45
*
45
- * @param array $apiKeys Some servers needs authentication. Provide your API keys here. (eg. array('GCM' => 'GCM_API_KEY'))
46
+ * @param array $auth Some servers needs authentication.
46
47
* @param array $defaultOptions TTL, urgency, topic
47
48
* @param int|null $timeout Timeout of POST request
48
49
* @param AbstractClient|null $client
49
50
*/
50
- public function __construct (array $ apiKeys = array (), $ defaultOptions = array (), $ timeout = 30 , AbstractClient $ client = null )
51
+ public function __construct (array $ auth = array (), $ defaultOptions = array (), $ timeout = 30 , AbstractClient $ client = null )
51
52
{
52
- $ this ->apiKeys = $ apiKeys ;
53
+ $ this ->auth = $ auth ;
53
54
$ this ->setDefaultOptions ($ defaultOptions );
54
55
55
56
$ client = isset ($ client ) ? $ client : new MultiCurl ();
@@ -204,11 +205,13 @@ private function prepareAndSend(array $notifications)
204
205
$ headers ['Topic ' ] = $ options ['topic ' ];
205
206
}
206
207
207
- if (substr ($ endpoint , 0 , Utils::safe_strlen (self ::GCM_URL )) === self ::GCM_URL ) {
208
- if (array_key_exists ('GCM ' , $ this ->apiKeys )) {
209
- $ headers ['Authorization ' ] = 'key= ' .$ this ->apiKeys ['GCM ' ];
208
+ // if GCM or FCM url
209
+ if (substr ($ endpoint , 0 , Utils::safe_strlen (self ::GCM_URL )) === self ::GCM_URL
210
+ || substr ($ endpoint , 0 , Utils::safe_strlen (self ::FCM_URL )) === self ::FCM_URL ) {
211
+ if (array_key_exists ('GCM ' , $ this ->auth )) {
212
+ $ headers ['Authorization ' ] = 'key= ' .$ this ->auth ['GCM ' ];
210
213
} else {
211
- throw new \ErrorException ('No GCM API Key specified. ' );
214
+ throw new \ErrorException ('No GCM/FCM API Key specified. ' );
212
215
}
213
216
}
214
217
0 commit comments