A Laravel package that lets you use the new FCM Http V1 API and send push notifications with ease.
If your firebase project is already setup, you can skip that part and go to the Usage section section.
The installation will take two steps. First we will build and manage the firebase project through the Firebase Console. Then we will see how you can implement the Firebase FCM Http V1 in your awesome Laravel project.
-
Go to the Firebase console.
-
Choose if you want to enable Analytics and create the project.
-
Go into the project settings of the app, switch to the Service accounts tab then click on Generate new private key. It will download a json file containing credentials for your app.
-
Go to project settings, cloud messaging tab and enable CloudMessaging API ( click on the 3 dots on the right, Manage API in Google Cloud Console, and enable the API)
-
Refresh firebase console page, a server key will be displayed under the Cloud Messaging API. (in Cloud Messaging tab)
Firebase configuration is now completed.
- Put the downloaded json at the root of the project. (JSON downloaded at step 7 of Firebase configuration)
- Go to Firebase Console -> Project Settings -> General and watch firebaseConfig.
- Assign values to the .env variables
FCM_API_KEY="<firebase apiKey>"
FCM_AUTH_DOMAIN="<firebase authDomain>"
FCM_PROJECT_ID="<firebase projectId>"
FCM_STORAGE_BUCKET="<firebase storageBucket>"
FCM_MESSAGIN_SENDER_ID="<firebase messagingSenderId>"
FCM_APP_ID="<firebase appId>"
FCM_JSON="<name of the json file downloaded at firebase step 7 install>"
FCM_API_SERVER_KEY=<api server key step 8-9 of firebase install>
- Package installation
composer require appy/fcmhttpv1
- Register the provider in config/app.php
Appy\FcmHttpV1\FcmProvider::class,
- Publish config file
php artisan vendor:publish --tag=fcmhttpv1 --ansi --force
-
Please follow this tutorial to configure Laravel PWA.
-
Create a file "firebase-messaging-sw.js" at public folder of your project.
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
databaseURL: 'https://project-id.firebaseio.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
Topics are used to make groups of device tokens. They will allow you to send notification directly to the topic where users are registered in.
To subscribe tokens to a topic :
use Appy\FcmHttpV1\FcmTopicHelper;
$tokens = ["first token", ... , "last token"];
FcmTopicHelper::subscribeToTopic($tokens, "myTopic");
use Appy\FcmHttpV1\FcmTopicHelper;
$tokens = ["first token", ... , "last token"];
FcmTopicHelper::unsubscribeToTopic($tokens, "myTopic");
use Appy\FcmHttpV1\FcmTopicHelper;
$token = "your awesome device token";
FcmTopicHelper::getTopicsByToken($token);
You can send notification to specific user or to topics.
use Appy\FcmHttpV1\FcmNotification;
$notif = new FcmNotification();
$notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setToken("put device token here")->setClickAction("/news")->send();
use Appy\FcmHttpV1\FcmNotification;
$notif = new FcmNotification();
$notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setTopic("general_topic")->setClickAction("/news")->send();