PHP code example of dive-be / laravel-expo-channel
1. Go to this page and download the library: Download dive-be/laravel-expo-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/ */
final 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(Carbon::now()->addHour())
->priority('high')
->playSound();
}
public function via($notifiable): array
{
return ['expo'];
}
}
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());
}
final class User extends Authenticatable
{
use Notifiable;
protected $casts = ['expo_token' => ExpoPushToken::class];
public function routeNotificationForExpo(): ?ExpoPushToken
{
return $this->expo_token;
}
}
final class User extends Authenticatable
{
use Notifiable;
/**
* @return Collection<int, ExpoPushToken>
*/
public function routeNotificationForExpo(): Collection
{
return $this->devices->pluck('expo_token');
}
}
$user->notify(new SuspiciousActivityDetected());
final class StoreDeviceRequest extends FormRequest
{
public function rules(): array
{
return [
'device_id' => ['
final class User extends Authenticatable
{
use Notifiable;
protected $casts = ['expo_token' => AsExpoPushToken::class];
}
final readonly 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...
}
}
}
final readonly class ExpoError
{
private function __construct(
public ExpoErrorType $type,
public ExpoPushToken $token,
public string $message,
) {}
}