PHP code example of laravel-notification-channels / fcm
1. Go to this page and download the library: Download laravel-notification-channels/fcm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laravel-notification-channels / fcm example snippets
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class AccountActivated extends Notification
{
public function via($notifiable)
{
return [FcmChannel::class];
}
public function toFcm($notifiable): FcmMessage
{
return (new FcmMessage(notification: new FcmNotification(
title: 'Account Activated',
body: 'Your account has been activated.',
image: 'http://example.com/url-to-image-here.png'
)))
->data(['data1' => 'value', 'data2' => 'value2'])
->custom([
'android' => [
'notification' => [
'color' => '#0A0A0A',
'sound' => 'default',
],
'fcm_options' => [
'analytics_label' => 'analytics',
],
],
'apns' => [
'payload' => [
'aps' => [
'sound' => 'default'
],
],
'fcm_options' => [
'analytics_label' => 'analytics',
],
],
]);
}
}
class User extends Authenticatable
{
use Notifiable;
...
/**
* Specifies the user's FCM token
*
* @return string|array
*/
public function routeNotificationForFcm()
{
return $this->fcm_token;
}
}
class User extends Authenticatable
{
use Notifiable;
...
/**
* Specifies the user's FCM tokens
*
* @return string|array
*/
public function routeNotificationForFcm()
{
return $this->getDeviceTokens();
}
}