Skip to content

Commit ffb0a1b

Browse files
committed
add support for FCM urls (uses the same GCM key) cf #40
1 parent 4040256 commit ffb0a1b

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/WebPush.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
class WebPush
2121
{
2222
const GCM_URL = 'https://android.googleapis.com/gcm/send';
23+
const FCM_URL = 'https://fcm.googleapis.com/fcm/send';
2324

2425
/** @var Browser */
2526
protected $browser;
2627

27-
/** @var array Key is push server type and value is the API key */
28-
protected $apiKeys;
28+
/** @var array */
29+
protected $auth;
2930

3031
/** @var array Array of array of Notifications */
3132
private $notifications;
@@ -42,14 +43,14 @@ class WebPush
4243
/**
4344
* WebPush constructor.
4445
*
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.
4647
* @param array $defaultOptions TTL, urgency, topic
4748
* @param int|null $timeout Timeout of POST request
4849
* @param AbstractClient|null $client
4950
*/
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)
5152
{
52-
$this->apiKeys = $apiKeys;
53+
$this->auth = $auth;
5354
$this->setDefaultOptions($defaultOptions);
5455

5556
$client = isset($client) ? $client : new MultiCurl();
@@ -204,11 +205,13 @@ private function prepareAndSend(array $notifications)
204205
$headers['Topic'] = $options['topic'];
205206
}
206207

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'];
210213
} else {
211-
throw new \ErrorException('No GCM API Key specified.');
214+
throw new \ErrorException('No GCM/FCM API Key specified.');
212215
}
213216
}
214217

0 commit comments

Comments
 (0)