/fcmhttpv1

Send push notifications with Laravel and the FCM Http V1 API

Primary LanguagePHP

Laravel FCM Http V1 API Package

A Laravel package that lets you use the new FCM Http V1 API and send push notifications with ease.

Summary

  1. Install
  2. Usage

Install

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.

Firebase

  1. Go to the Firebase console.

  2. Create a project
    Capture d’écran 2022-06-30 143010

  3. Add a name
    Capture d’écran 2022-07-08 102739

  4. Choose if you want to enable Analytics and create the project.

  5. Add a Web App
    Capture d’écran 2022-07-08 103535

  6. Add an app nickname
    Capture d’écran 2022-07-08 103625

  7. 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.

  8. 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)
    Capture d’écran 2022-07-08 142946

  9. Refresh firebase console page, a server key will be displayed under the Cloud Messaging API. (in Cloud Messaging tab)

Firebase configuration is now completed.

Laravel

  1. Put the downloaded json at the root of the project. (JSON downloaded at step 7 of Firebase configuration)
    Capture d’écran 2022-07-08 144029
  2. Go to Firebase Console -> Project Settings -> General and watch firebaseConfig.
    Capture d’écran 2022-07-08 144454
  3. 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>
  1. Package installation
composer require appy/fcmhttpv1
  1. Register the provider in config/app.php
Appy\FcmHttpV1\FcmProvider::class,
  1. Publish config file
php artisan vendor:publish --tag=fcmhttpv1 --ansi --force

Laravel PWA

  1. Please follow this tutorial to configure Laravel PWA.

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

Usage

Topics

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.

Subscribe

To subscribe tokens to a topic :

use Appy\FcmHttpV1\FcmTopicHelper;

$tokens = ["first token", ... , "last token"];
FcmTopicHelper::subscribeToTopic($tokens, "myTopic");

Unsubscribe

use Appy\FcmHttpV1\FcmTopicHelper;

$tokens = ["first token", ... , "last token"];
FcmTopicHelper::unsubscribeToTopic($tokens, "myTopic");

List subscriptions

use Appy\FcmHttpV1\FcmTopicHelper;

$token = "your awesome device token";
FcmTopicHelper::getTopicsByToken($token);

Notification

You can send notification to specific user or to topics.

Send to unique token

use Appy\FcmHttpV1\FcmNotification;

$notif = new FcmNotification();
$notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setToken("put device token here")->setClickAction("/news")->send();

Send to topic

use Appy\FcmHttpV1\FcmNotification;

$notif = new FcmNotification();
$notif->setTitle("Title")->setBody("Message here")->setIcon("icon.png")->setTopic("general_topic")->setClickAction("/news")->send();