Some Firebase functionalities require that you write a server application in order to use them.
This is very simple PHP application that exposes some useful API endpoints that you can use from your mobile or web app.
- For example, sending multiple Notifications from one device to several other devices can not be done without a server app.
- This app uses Firebase Admin SKD for PHP under the hood
- Minimal PHP version is v7.2
- Apache
composer create-project mi5ha/firebase-backend-api
- Download service account key file from Firebase
- Go to "Settings -> Service accounts"
- First click "Create service account" then "Generate new private key"
- Put service key file somewhere outside document root
- Open
config/config.php
, and add the path to your service key relative to config file
- Its important that your service account key, i.e. json file you downloaded from Firebase, is not accessible from the net
- If you accidentally put this file inside this project, there is .htaccess that should prevent user accessing it (Apache only)
- At the moment this application doesn't support any kind of API authentication
With all requests send this header:
Content-Type: application/json
Send notifications from one device to multiple other devices.
Example
POST request
http://<your-domain>/?method=sendMulticastMessages
JSON request body
{
"title": "I am Iron Man",
"deviceTokens": [
"fqORTS66YYQHKnpSsdf0QIKl:APA91bEggEnA-NDaSRvFtHsdd3UQLw3miPSw0jINjrgg0DdaRUP9u2DXBE6Veq2br9qsmDI5q2S-VnA1SvSmBnrOKMCxyuLzhh0EY2YXvQRqrDL5nf5FC8sjjLAgenLm-voggrtjAdlW",
"f4fRp143affMgijGpVoj5I:APA91bGaaeqJ1IwtTmVvPVahdzeFbffvXizaL1u2iYGqWDhhht0aJuEFDt-qNHHHTgggUGfrM6qcrwKffFz7Sm-2PMsCFfkjjQjcNDbnn_tOcu9AF0OwGX21aaOpbXCUhhGyy4o5Zcr"
],
"imageUrl": "https://i.insider.com/5b52400e51dfbe20008b45f9?width=750&format=jpeg&auto=webp"
}
Success response
{
"success": true
}
Error response
{
"success": false,
"errorMessages": [
"The registration token is not a valid FCM registration token",
"The registration token is not a valid FCM registration token"
]
}
errorMessages is an array of specific error messages for each device token.
You need to get device tokens to use this method
For example from React Native Firebase you can get this token with getToken()
import messaging from '@react-native-firebase/messaging';
let deviceToken = await messaging().getToken();
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Firebase PHP Backend is licensed under the MIT License.
Your use of Firebase is governed by the Terms of Service for Firebase Services.