diff --git a/.whitesource b/.whitesource new file mode 100644 index 0000000..0255b87 --- /dev/null +++ b/.whitesource @@ -0,0 +1,3 @@ +{ + "settingsInheritedFrom": "vanderbilt-redcap/whitesource-config@main" +} \ No newline at end of file diff --git a/composer.json b/composer.json index 8baa766..00b2a58 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,17 @@ { - "name": "sngrl/php-firebase-cloud-messaging", + "name": "vanderbilt-redcap/php-firebase-cloud-messaging", "description": "PHP API for Firebase Cloud Messaging from Google", "license": "MIT", "keywords": ["PHP","FCM","Google","Firebase","Cloud","Notifications","Android","iOS","Chrome"], - "homepage": "https://github.com/sngrl/php-firebase-cloud-messaging", + "homepage": "https://github.com/vanderbilt-redcap/php-firebase-cloud-messaging", "authors": [ { - "name": "Sngrl", - "email": "i@sngrl.ru" + "name": "Vaishali Jagtap", + "email": "vaishali.jagtap@vumc.org" } ], "require": { - "guzzlehttp/guzzle": "*", + "guzzlehttp/guzzle": "^7.8", "php": ">=5.5" }, "require-dev": { @@ -20,12 +20,12 @@ }, "autoload": { "psr-4": { - "sngrl\\PhpFirebaseCloudMessaging\\": "src/" + "Vanderbilt\\PhpFirebaseCloudMessaging\\": "src/" } }, "autoload-dev": { "psr-4": { - "sngrl\\PhpFirebaseCloudMessaging\\Tests\\": "tests/" + "Vanderbilt\\PhpFirebaseCloudMessaging\\Tests\\": "tests/" } } } \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 5125536..20cb153 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,18 +1,22 @@ accessToken = $accessToken; + return $this; + } /** * sends your notification to the google servers and returns a guzzle repsonse object * containing their answer. @@ -59,16 +75,41 @@ public function setProxyApiUrl($url) */ public function send(Message $message) { - return $this->guzzleClient->post( - $this->getApiUrl(), - [ - 'headers' => [ - 'Authorization' => sprintf('key=%s', $this->apiKey), - 'Content-Type' => 'application/json' - ], - 'body' => json_encode($message) - ] - ); + $param = ['message' => $message]; + + // FCM HTTP V1 does not support sending notifications to multiple devices (supported only in legacy API via registration_tokens + // So, sending notification to devices in a loop (It will be rare case where same participant joined from different devices) + $recipients = $message->getRecipients(); + if (count($recipients) > 1) { + foreach ($recipients as $recipient) { + $token = $recipient->getToken(); + $messageArr = json_decode(json_encode($param), true); + unset($messageArr['message']['registration_ids']); + $messageArr['message']['token'] = $token; + $output = $this->guzzleClient->post( + $this->getHTTPV1ApiUrl(), + [ + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + 'Content-Type' => 'application/json' + ], + 'body' => json_encode($messageArr) + ] + ); + } + return $output; + } else { + return $this->guzzleClient->post( + $this->getHTTPV1ApiUrl(), + [ + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + 'Content-Type' => 'application/json' + ], + 'body' => json_encode($param) + ] + ); + } } /** @@ -111,7 +152,8 @@ protected function processTopicSubscription($topic_id, $recipients_tokens, $url) $url, [ 'headers' => [ - 'Authorization' => sprintf('key=%s', $this->apiKey), + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + 'access_token_auth' => true, 'Content-Type' => 'application/json' ], 'body' => json_encode([ @@ -127,4 +169,25 @@ private function getApiUrl() { return isset($this->proxyApiUrl) ? $this->proxyApiUrl : self::DEFAULT_API_URL; } + + /** + * add your project ID + * + * @param string $projectId + * + * @return \Vanderbilt\PhpFirebaseCloudMessaging\Client + */ + public function setProjectId($projectId) + { + $this->projectId = $projectId; + } + private function getHTTPV1ApiUrl() + { + return self::HTTPV1_API_URL_PREFIX.$this->getProjectId().self::HTTPV1_API_URL_POSTEFIX; + } + + public function getProjectId() + { + return $this->projectId; + } } \ No newline at end of file diff --git a/src/ClientInterface.php b/src/ClientInterface.php index f537401..207784c 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -1,11 +1,11 @@ priority = $priority; return $this; } - + public function setAndroidPriority(array $priority) + { + $this->android = $priority; + return $this; + } public function setData(array $data) { $this->data = $data; @@ -166,7 +171,11 @@ public function jsonSerialize() } if (count($this->recipients) == 1) { - $jsonData['to'] = $this->createTarget(); + if ($this->recipientType == Device::class) { + $jsonData['token'] = $this->createTarget(); + } else { + $jsonData['topic'] = $this->createTarget(); + } } elseif ($this->recipientType == Device::class) { $jsonData['registration_ids'] = $this->createTarget(); } else { @@ -185,6 +194,9 @@ public function jsonSerialize() if ($this->notification) { $jsonData['notification'] = $this->notification; } + if ($this->android) { + $jsonData['android'] = $this->android; + } return $jsonData; } @@ -198,7 +210,7 @@ private function createTarget() case Topic::class: if ($recipientCount == 1) { - return sprintf('/topics/%s', current($this->recipients)->getName()); + return sprintf('%s', current($this->recipients)->getName()); } else if ($recipientCount > self::MAX_TOPICS) { throw new \OutOfRangeException(sprintf('Message topic limit exceeded. Firebase supports a maximum of %u topics.', self::MAX_TOPICS)); @@ -240,4 +252,9 @@ private function createTarget() } return null; } + + /* Get All recipients */ + public function getRecipients() { + return $this->recipients; + } } \ No newline at end of file diff --git a/src/Notification.php b/src/Notification.php index ad18565..48fb191 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -1,5 +1,5 @@