Firebase Cloud Messaging for Laravel


February 9th, 2021

Firebase Cloud Messaging for Laravel

Larafirebase is a package by Gentrit Abazi that provides sending push notifications and custom messages with Firebase in Laravel applications. This package uses Firebase Cloud Messaging—a cross-platform messaging solution to send messages at no cost—to send notifications to users on the client-side.

Here is an example notification class that provides the toFirebase() method to send notifications via the custom firebase channel:

1use Illuminate\Notifications\Notification;
2use Kutia\Larafirebase\Messages\FirebaseMessage;
4class SendBirthdayReminder extends Notification
6 /**
7 * Get the notification's delivery channels.
8 */
9 public function via($notifiable)
10 {
11 return ['firebase'];
12 }
14 /**
15 * Get the firebase representation of the notification.
16 */
17 public function toFirebase($notifiable)
18 {
19 $deviceTokens = [
20 '{TOKEN_1}',
21 '{TOKEN_2}'
22 ];
24 return (new FirebaseMessage)
25 ->withTitle('Hey, ', $notifiable->first_name)
26 ->withBody('Happy Birthday!')
27 ->asNotification($deviceTokens);
28 // OR ->asMessage($deviceTokens);
29 }

You'll also need to write client code to receive these messages to use this package. Check out the project's javascript-client folder for an example of how to use this package on the client-side.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.