PHP code example of ankurk91 / fcm-notification-channel
1. Go to this page and download the library: Download ankurk91/fcm-notification-channel 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/ */
ankurk91 / fcm-notification-channel example snippets
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\FCM\FCMChannel;
use Kreait\Firebase\Messaging\CloudMessage;
class ExampleNotification extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable): array
{
return [FCMChannel::class];
}
public function toFCM($notifiable): CloudMessage
{
return CloudMessage::new()
->withDefaultSounds()
->withNotification([
'title' => 'Order shipped',
'body' => 'Your order for laptop is shipped.',
])
->withData([
'orderId' => '#123'
]);
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Assuming that you have a database table which stores device tokens.
*/
public function deviceTokens(): HasMany
{
return $this->hasMany(DeviceToken::class);
}
public function routeNotificationForFCM($notification): string|array|null
{
return $this->deviceTokens->pluck('token')->toArray();
}
/**
* Optional method to determine which message target to use
* We will use TOKEN type when not specified
* @see \Kreait\Firebase\Messaging\MessageTarget::TYPES
*/
public function routeNotificationForFCMTargetType($notification): ?string
{
return \Kreait\Firebase\Messaging\MessageTarget::TOKEN;
}
/**
* Optional method to determine which Firebase project to use
* We will use default project when not specified
*/
public function routeNotificationForFCMProject($notification): ?string
{
return config('firebase.default');
}
}
use Illuminate\Support\Facades\Notification;
use Kreait\Firebase\Messaging\MessageTarget;
use App\Notification\ExampleNotification;
Notification::route('FCM', 'topicA')
->route('FCMTargetType', MessageTarget::TOPIC)
->notify(new ExampleNotification());
Notification::route('FCM', "'TopicA' in topics")
->route('FCMTargetType', MessageTarget::CONDITION)
->notify(new ExampleNotification());
Notification::route('FCM', ['token_1', 'token_2'])
->route('FCMTargetType', MessageTarget::TOKEN)
->notify(new ExampleNotification());
namespace App\Providers;
use Illuminate\Notifications\Events;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Events\NotificationSent::class => [
//\App\Listeners\FCMNotificationSent::class,
],
Events\NotificationFailed::class => [
\App\Listeners\FCMNotificationFailed::class,
],
];
}
namespace App\Listeners;
use App\Models\User;
use Illuminate\Support\Arr;
use NotificationChannels\FCM\FCMChannel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Kreait\Laravel\Firebase\Facades\Firebase;
use Illuminate\Notifications\Events\NotificationFailed;
class FCMNotificationFailed implements ShouldQueue
{
public function handle(NotificationFailed $event)
{
if ($event->channel !== FCMChannel::class) {
return;
}
/** @var User $user */
$user = $event->notifiable;
$invalidTokens = $this->findInvalidTokens($user);
if (count($invalidTokens)) {
$user->deviceTokens()->whereIn('token', $invalidTokens)->delete();
}
}
protected function findInvalidTokens(User $user): array
{
$tokens = Arr::wrap($user->routeNotificationFor('FCM'));
if (! count($tokens)) {
return [];
}
$project = $user->routeNotificationFor('FCMProject');
$response = Firebase::project($project)->messaging()->validateRegistrationTokens($tokens);
return array_unique(array_merge($response['invalid'], $response['unknown']));
}
}