PHP code example of laravel-notification-channels / expo
1. Go to this page and download the library: Download laravel-notification-channels/expo 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 / expo example snippets
use NotificationChannels\Expo\ExpoMessage;
class SuspiciousActivityDetected extends Notification
{
public function toExpo($notifiable): ExpoMessage
{
return ExpoMessage::create('Suspicious Activity')
->body('Someone tried logging in to your account!')
->data($notifiable->only('email', 'id'))
->expiresAt(now()->addHour())
->priority('high')
->playSound();
}
public function via($notifiable): array
{
return ['expo'];
}
}
use NotificationChannels\Expo\ExpoMessage;
public function toExpo($notifiable): ExpoMessage
{
return ExpoMessage::create('Suspicious Activity')
->body('Someone tried logging in to your account!')
->when($notifiable->wantsSound(), fn ($msg) => $msg->playSound())
->unless($notifiable->isVip(), fn ($msg) => $msg->normal(), fn ($msg) => $msg->high());
}
use NotificationChannels\Expo\ExpoPushToken;
class User extends Authenticatable
{
use Notifiable;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'expo_token' => ExpoPushToken::class
];
}
public function routeNotificationForExpo(): ?ExpoPushToken
{
return $this->expo_token;
}
}
use Illuminate\Database\Eloquent\Collection;
class User extends Authenticatable
{
use Notifiable;
/**
* @return Collection<int, ExpoPushToken>
*/
public function routeNotificationForExpo(): Collection
{
return $this->devices->pluck('expo_token');
}
}
$user->notify(new SuspiciousActivityDetected());
use NotificationChannels\Expo\ExpoPushToken;
class StoreDeviceRequest extends FormRequest
{
public function rules(): array
{
return [
'device_id' => ['
use NotificationChannels\Expo\ExpoPushToken;
class User extends Authenticatable
{
use Notifiable;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'expo_token' => ExpoPushToken::class
];
}
}
use Illuminate\Notifications\Events\NotificationFailed;
class HandleFailedExpoNotifications
{
public function handle(NotificationFailed $event)
{
if ($event->channel !== 'expo') return;
/** @var ExpoError $error */
$error = $event->data;
// Remove old token
if ($error->type->isDeviceNotRegistered()) {
$event->notifiable->update(['expo_token' => null]);
} else {
// do something else like logging...
}
}
}
namespace NotificationChannels\Expo;
final readonly class ExpoError
{
private function __construct(
public ExpoErrorType $type,
public ExpoPushToken $token,
public string $message,
) {}
}