Send a push notification to user mobile, and web using FCM (Firebase Cloud Messaging).
composer require heyharpreetsingh/fcm
Next, Register the service provider in file bootstrap/providers.php
for L11 AND config/app.php
in the providers
key for l10
Heyharpreetsingh\FCM\Providers\FCMServiceProvider::class
Next, Generate new private key from your Firebase:
In order to get this file, let’s go to your Firebase projects page, then select your project and as shown below, go to Project Settings
.
Click on the Service accounts
tab, then Generate new private key.
A file named -firebase-adminsdk-.json get generated, and it is your ServiceAccount.json file! Rename it (or change the path) and you’re ready to go.
Next, Put your ServiceAccount.json
file in your project storage directory and add the name of json file in your project .env
file:
FCM_GOOGLE_APPLICATION_CREDENTIALS=path/to/serviceAccountKey.json
use a FCMFacade
to send a notification
use Heyharpreetsingh\FCM\Facades\FCMFacade;
FCMFacade::send([
"message" => [
"token" => "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", //device token
"notification" => [
"body" => "This is an FCM notification message!",
"title" => "FCM Message"
]
]
]);
You can pass additional data using the official doc of FCM.
- https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send
- https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Message
If you have any issue or suggestion feel free to contact.
Open-sourced software licensed under the MIT license.