subit / expo-laravel
Laravel的Expo通知驱动
v5.4.2
2024-09-19 14:38 UTC
Requires
- php: >=7.4
- ext-json: >=7.4
- laravel/framework: ^6.0||^7.0||^8.0||^9.0||^10.0
- subit/php-expo-sdk: >=2.1.0
Requires (Dev)
- mockery/mockery: ^1.3
- orchestra/testbench: ^6.3
- phpunit/phpunit: 9.5.0
README
内容
安装
您可以通过composer安装此包
composer require subit-io/laravel-expo-notifications
如果您正在使用Laravel 5.5或更高版本,此包将自动使用包发现注册自身。对于Laravel的旧版本,您必须手动安装服务提供者
// config/app.php 'providers' => [ ... NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider::class, ],
在发布exponent通知迁移之前,您必须在.env文件中添加
EXPONENT_PUSH_NOTIFICATION_RECIPIENTS_STORAGE_DRIVER=database
您可以使用以下命令发布迁移
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="migrations"
在发布迁移后,您可以通过运行迁移来创建expo_notification_recipients
表
php artisan migrate
您可以选择使用以下命令发布配置文件
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="config"
这是发布的配置文件内容
return [ 'recipients' => [ /* * Supported: "database" */ 'driver' => env('EXPONENT_PUSH_NOTIFICATION_RECIPIENTS_STORAGE_DRIVER', 'file'), 'database' => [ 'table_name' => 'expo_notification_recipients', ], ] ];
用法
use NotificationChannels\ExpoPushNotifications\ExpoChannel; use NotificationChannels\ExpoPushNotifications\ExpoMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via($notifiable) { return [ExpoChannel::class]; } public function toExpoPush($notifiable) { return ExpoMessage::create() ->badge(1) ->enableSound() ->title("Congratulations!") ->body("Your {$notifiable->service} account was approved!"); } }
ExpoMessage
选项
有关更详细的描述,请参阅Expo文档https://docs.expo.io/versions/latest/guides/push-notifications/#formats
管理接收者
此包注册了两个端点来处理接收者的订阅,端点在src/Http/routes.php文件中定义,由ExpoController使用,并通过包服务提供者加载。
路由消息
默认情况下,Expo "接收者"消息将发送到的ID将使用(notifiable类作为类型)定义,例如App\User
。但是,您可以通过在notifiable类方法中包含一个routeNotificationForExpoPushNotifications()
来更改此行为,该方法返回接收者类型。