Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3f5a6d2
Update composer.json file
vaishali-vumc May 10, 2024
fd3e713
Updated namespace from sngrl to Vanderbilt
vaishali-vumc May 10, 2024
969dc55
Debug input param
vaishali-vumc May 11, 2024
79a77ef
Debug output
vaishali-vumc May 11, 2024
afaf7cc
Remove Debug output
vaishali-vumc May 11, 2024
6d6e179
Debug input
vaishali-vumc May 11, 2024
1ad004c
Set message
vaishali-vumc May 11, 2024
8110752
Set message
vaishali-vumc May 11, 2024
9cd41c8
remove Set message
vaishali-vumc May 11, 2024
bf262a1
Debug request array
vaishali-vumc May 13, 2024
a56d8cd
Debug request array
vaishali-vumc May 13, 2024
9f77047
Remove Debug request array
vaishali-vumc May 13, 2024
829e7e1
Debug request array
vaishali-vumc May 13, 2024
0106593
Removed Debug request array
vaishali-vumc May 13, 2024
d9b8d15
Migrate from legacy FCM APIs to HTTP v1
vaishali-vumc May 20, 2024
4484caa
Alternative to "registered_ids is not supported in http v1" to send n…
vaishali-vumc May 21, 2024
1d2b201
Set priority for android devices as "priority='high'" is not supporte…
vaishali-vumc May 21, 2024
80d2ab8
Removed debug print array line
vaishali-vumc May 22, 2024
5e12221
Debug response
vaishali-vumc May 22, 2024
7489d46
Removed debug print array line
vaishali-vumc May 22, 2024
cacfa55
Debug
vaishali-vumc May 22, 2024
742fc78
Removed debug print array line
vaishali-vumc May 22, 2024
a779475
debug
vaishali-vumc May 22, 2024
4be2072
debug
vaishali-vumc May 22, 2024
feaff36
debug
vaishali-vumc May 22, 2024
1c87f26
Assigned topic name to projectCode
vaishali-vumc May 22, 2024
e365627
debug
vaishali-vumc May 22, 2024
6b7eae7
Unset participantCode
vaishali-vumc May 22, 2024
e388e64
Removed debug print array line
vaishali-vumc May 22, 2024
9f6caee
remove topic subscription only when response is OK
vaishali-vumc May 22, 2024
8e83671
Debug
vaishali-vumc May 22, 2024
e0f9e0b
Debug
vaishali-vumc May 22, 2024
cddf6d8
Debug
vaishali-vumc May 22, 2024
c84212d
Debug
vaishali-vumc May 22, 2024
daa1150
Merge pull request #1 from vanderbilt-redcap/firebase-http-v1
vaishali-vumc May 29, 2024
89d6055
Fix for Sending notification to multiple devices at same time
vaishali-vumc May 30, 2024
d90e059
Merge pull request #2 from vanderbilt-redcap/fix-send-to-many-devices
vaishali-vumc May 30, 2024
2a45913
Add .whitesource configuration file
mend-for-github-com[bot] Aug 1, 2025
34bae18
Merge pull request #3 from vanderbilt-redcap/whitesource/configure
mtmirse Aug 1, 2025
c1d937d
Update to Guzzle 7
vaishali-vumc Jun 29, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"settingsInheritedFrom": "vanderbilt-redcap/whitesource-config@main"
}
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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/"
}
}
}
93 changes: 78 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
namespace Vanderbilt\PhpFirebaseCloudMessaging;

use GuzzleHttp;

/**
* @author sngrl
* @author Vanderbilt
*/
class Client implements ClientInterface
{
const DEFAULT_API_URL = 'https://fcm.googleapis.com/fcm/send';
const HTTPV1_API_URL_PREFIX = 'https://fcm.googleapis.com/v1/projects/';
const HTTPV1_API_URL_POSTEFIX = '/messages:send';
const DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchAdd';
const DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchRemove';

private $apiKey;
private $accessToken;
private $projectId;
private $proxyApiUrl;
private $guzzleClient;

Expand All @@ -27,7 +31,7 @@ public function injectGuzzleHttpClient(GuzzleHttp\ClientInterface $client)
*
* @param string $apiKey
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Client
*/
public function setApiKey($apiKey)
{
Expand All @@ -40,14 +44,26 @@ public function setApiKey($apiKey)
*
* @param string $url
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Client
*/
public function setProxyApiUrl($url)
{
$this->proxyApiUrl = $url;
return $this;
}

/**
* add your server access token here
*
* @param string $accessToken
*
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Client
*/
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
return $this;
}
/**
* sends your notification to the google servers and returns a guzzle repsonse object
* containing their answer.
Expand All @@ -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)
]
);
}
}

/**
Expand Down Expand Up @@ -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([
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
namespace Vanderbilt\PhpFirebaseCloudMessaging;

use GuzzleHttp;

/**
*
* @author sngrl
* @author Vanderbilt
*
*/
interface ClientInterface
Expand All @@ -17,7 +17,7 @@ interface ClientInterface
*
* @param string $apiKey
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Client
*/
function setApiKey($apiKey);

Expand All @@ -27,7 +27,7 @@ function setApiKey($apiKey);
*
* @param string $url
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Client
*/
function setProxyApiUrl($url);

Expand Down
37 changes: 27 additions & 10 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
namespace Vanderbilt\PhpFirebaseCloudMessaging;

use sngrl\PhpFirebaseCloudMessaging\Recipient\Recipient;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Recipient;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Topic;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Device;

/**
* @author sngrl
* @author Vanderbilt
*/
class Message implements \JsonSerializable
{
Expand All @@ -15,10 +15,11 @@ class Message implements \JsonSerializable
*/
const MAX_TOPICS = 3;
const MAX_DEVICES = 1000;

private $notification;
private $collapseKey;
private $priority;
private $android;
private $data;
private $recipients = [];
private $recipientType;
Expand All @@ -35,7 +36,7 @@ public function __construct() {
*
* @param Recipient $recipient
*
* @return \sngrl\PhpFirebaseCloudMessaging\Message
* @return \Vanderbilt\PhpFirebaseCloudMessaging\Message
*/
public function addRecipient(Recipient $recipient)
{
Expand Down Expand Up @@ -68,7 +69,11 @@ public function setPriority($priority)
$this->priority = $priority;
return $this;
}

public function setAndroidPriority(array $priority)
{
$this->android = $priority;
return $this;
}
public function setData(array $data)
{
$this->data = $data;
Expand Down Expand Up @@ -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 {
Expand All @@ -185,6 +194,9 @@ public function jsonSerialize()
if ($this->notification) {
$jsonData['notification'] = $this->notification;
}
if ($this->android) {
$jsonData['android'] = $this->android;
}

return $jsonData;
}
Expand All @@ -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));
Expand Down Expand Up @@ -240,4 +252,9 @@ private function createTarget()
}
return null;
}

/* Get All recipients */
public function getRecipients() {
return $this->recipients;
}
}
2 changes: 1 addition & 1 deletion src/Notification.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
namespace Vanderbilt\PhpFirebaseCloudMessaging;

/**
* @link https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
Expand Down
2 changes: 1 addition & 1 deletion src/Recipient/Device.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Recipient;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Recipient;

class Device extends Recipient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Recipient/Recipient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Recipient;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Recipient;

class Recipient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Recipient/Topic.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Recipient;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Recipient;

class Topic extends Recipient
{
Expand Down
8 changes: 4 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Tests;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Tests;

use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic;
use sngrl\PhpFirebaseCloudMessaging\Message;
use Vanderbilt\PhpFirebaseCloudMessaging\Client;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Topic;
use Vanderbilt\PhpFirebaseCloudMessaging\Message;

use GuzzleHttp;
use GuzzleHttp\Psr7\Response;
Expand Down
12 changes: 6 additions & 6 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Tests;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Tests;

use sngrl\PhpFirebaseCloudMessaging\Recipient\Recipient;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic;
use sngrl\PhpFirebaseCloudMessaging\Notification;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Recipient;
use Vanderbilt\PhpFirebaseCloudMessaging\Message;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Topic;
use Vanderbilt\PhpFirebaseCloudMessaging\Notification;
use Vanderbilt\PhpFirebaseCloudMessaging\Recipient\Device;

class MessageTest extends PhpFirebaseCloudMessagingTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Tests;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Tests;

use sngrl\PhpFirebaseCloudMessaging\Notification;
use Vanderbilt\PhpFirebaseCloudMessaging\Notification;

class NotificationTest extends PhpFirebaseCloudMessagingTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpFirebaseCloudMessagingTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace sngrl\PhpFirebaseCloudMessaging\Tests;
namespace Vanderbilt\PhpFirebaseCloudMessaging\Tests;

class PhpFirebaseCloudMessagingTestCase extends \PHPUnit_Framework_TestCase
{
Expand Down