Skip to content

Commit 4a9b42f

Browse files
committed
first commit
0 parents  commit 4a9b42f

8 files changed

+207
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/composer.phar
2+
/composer.lock
3+
/phpunit.xml
4+
/vendor/
5+
/.phpunit.result.cache

.htaccess

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Options All -Indexes
2+
3+
Order Deny,Allow
4+
Deny from all
5+
6+
<FilesMatch "^(/)?$">
7+
Order Allow,Deny
8+
Allow from all
9+
</FilesMatch>
10+
11+
<Files index.php>
12+
Order Allow,Deny
13+
Allow from all
14+
</Files>

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## REQUIREMENTS
2+
3+
- PHP 7.2

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "mi5ha/firebase-backend-api",
3+
"description": "Backend API for Firebase",
4+
"version": "1.0.0",
5+
"authors": [
6+
{
7+
"name": "Miodrag Peric",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"type": "library",
12+
"keywords": ["codeception", "module", "psr-3", "psr", "log", "test"],
13+
"license": "MIT",
14+
"require": {
15+
"php": ">=7.2.0 <8.0",
16+
"kreait/firebase-php": "^5.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"FirebaseBackend\\": "src/"
21+
}
22+
}
23+
}

config/config.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
define("SHOW_ERRORS", false);
4+
5+
define(
6+
"FIREBASE_SERVICE_ACCOUNT_KEY",
7+
dirname(__FILE__) . '/XXXX-firebase-adminsdk-XXXX.json'
8+
);

index.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
require __DIR__ . '/vendor/autoload.php';
3+
require(dirname(__FILE__) . '/config/config.php');
4+
5+
use FirebaseBackend\API;
6+
use FirebaseBackend\CloudMessaging;
7+
8+
// ERROR HANDLING
9+
if (SHOW_ERRORS) {
10+
ini_set('display_errors', 1);
11+
ini_set('display_startup_errors', 1);
12+
error_reporting(E_ALL);
13+
}
14+
15+
// SET API METHOD
16+
$apiMethod = null;
17+
18+
if (isset($_GET['method'])) $apiMethod = $_GET['method'];
19+
20+
if (!isset($apiMethod)) {
21+
API::response(false, null, ['You must give an API method']);
22+
return;
23+
}
24+
25+
// CALL API METHOD
26+
if ($apiMethod === 'sendMulticast') {
27+
CloudMessaging::sendMulticast();
28+
} else {
29+
API::response(false, null, ['Given method doesnt exist']);
30+
}

src/API.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace FirebaseBackend;
4+
5+
class API
6+
{
7+
public static function response($success, $data = null, $errorMessages = null)
8+
{
9+
$response = [
10+
"success" => $success
11+
];
12+
13+
if (isset($data)) $response["data"] = $data;
14+
if (isset($errorMessages)) $response["errorMessages"] = $errorMessages;
15+
16+
header('Content-Type: application/json');
17+
print_r(json_encode($response));
18+
}
19+
20+
public static function getPostParams()
21+
{
22+
$jsonRequest = json_decode(file_get_contents('php://input'));
23+
return $jsonRequest ? $jsonRequest : null;
24+
}
25+
}

src/CloudMessaging.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
namespace FirebaseBackend;
3+
4+
require(dirname(__FILE__) . '/../config/config.php');
5+
6+
use FirebaseBackend\API;
7+
use Kreait\Firebase\Factory;
8+
use Kreait\Firebase\Messaging\CloudMessage;
9+
use Kreait\Firebase\Messaging\RawMessageFromArray;
10+
11+
class CloudMessaging
12+
{
13+
public static function sendMulticast()
14+
{
15+
// SET POST PARAMS
16+
// ---------------
17+
$postParams = API::getPostParams();
18+
19+
if ($postParams === null) {
20+
API::response(false, null, ['POST body must be a properly formatted JSON']);
21+
return;
22+
}
23+
24+
// SET API PARAMS
25+
// --------------
26+
$deviceTokens = isset($postParams->deviceTokens) ? $postParams->deviceTokens : null;
27+
$notificationTitle = isset($postParams->title) ? $postParams->title : null;
28+
$notificationText = isset($postParams->text) ? $postParams->text : null;
29+
$notificationimageUrl = isset($postParams->imageUrl) ? $postParams->imageUrl : null;
30+
31+
// VALIDATIONS
32+
// -----------
33+
$errorMessages = [];
34+
35+
// deviceTokens
36+
if (!$deviceTokens || count($deviceTokens) === 0) {
37+
$errorMessages['deviceTokens'] = 'At least one device token must be given';
38+
}
39+
40+
// notificationTitle
41+
if (!$notificationTitle) {
42+
$errorMessages['title'] = 'Notification title must be given';
43+
}
44+
45+
// RETURN ERROR RESPONSE
46+
// ---------------------
47+
if (count($errorMessages) > 0) {
48+
API::response(false, null, $errorMessages);
49+
return;
50+
}
51+
52+
// SEND NOTIFICATION
53+
// -----------------
54+
$factory = (new Factory)->withServiceAccount(FIREBASE_SERVICE_ACCOUNT_KEY);
55+
$messaging = $factory->createMessaging();
56+
57+
// $messageObject = CloudMessage::new();
58+
// $message = $messageObject->withNotification([
59+
// "title" => $notificationTitle,
60+
// "body" => $notificationText,
61+
// "image" => $notificationimageUrl,
62+
// ]);
63+
64+
// API: https://firebase-php.readthedocs.io/en/5.9.0/cloud-messaging.html#sending-a-fully-configured-raw-message
65+
$message = new RawMessageFromArray([
66+
"notification" => [
67+
"title" => $notificationTitle,
68+
"body" => $notificationText,
69+
"image" => $notificationimageUrl,
70+
],
71+
]);
72+
$report = $messaging->sendMulticast($message, $deviceTokens);
73+
74+
// Handle error
75+
$successCount = $report->successes()->count();
76+
$errorCount = $report->failures()->count();
77+
78+
$errorMessages = [];
79+
if ($report->hasFailures()) {
80+
foreach ($report->failures()->getItems() as $failure) {
81+
$errorMessages[] = $failure->error()->getMessage();
82+
}
83+
}
84+
85+
if (count($errorMessages) > 0) {
86+
API::response(
87+
false,
88+
null,
89+
$errorMessages
90+
);
91+
return;
92+
}
93+
94+
// SUCCESS RESPONSE
95+
API::response(
96+
true
97+
);
98+
}
99+
}

0 commit comments

Comments
 (0)