Skip to content

API endpoints for Firebase functionalities that require Firebase Admin SDK.

License

Notifications You must be signed in to change notification settings

mi5ha/firebase-php-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Firebase PHP Backend

Supported PHP version

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.

Table of contents

Overview

  • 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

Requirements

  • Minimal PHP version is v7.2
  • Apache

Installation

Create new project

composer create-project mi5ha/firebase-backend-api

Configuration

  • 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

Security Warning

  • 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

API

With all requests send this header:

Content-Type: application/json

sendMulticastMessages

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();

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Firebase PHP Backend is licensed under the MIT License.

Your use of Firebase is governed by the Terms of Service for Firebase Services.

About

API endpoints for Firebase functionalities that require Firebase Admin SDK.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages