ronasit/laravel-exponent-push-notifications
Composer 安装命令:
composer require ronasit/laravel-exponent-push-notifications
包简介
Exponent push notifications driver for laravel
README 文档
README
This package provides an Expo push notification channel for Laravel, allowing you to send notifications to mobile devices via the Expo SDK.
Contents
Installation
Install the package via composer:
composer require ronasit/laravel-exponent-push-notifications
Publish the config file (optionally):
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="config"
Storage Drivers
Package provides several drivers to store the push tokens:
| Driver | When to use |
|---|---|
file (default) |
Simple setups, single-server, no database required |
database |
Multi-server deployments, persistent storage, flexible queries |
Storage driver may be configured via the EXPONENT_PUSH_NOTIFICATION_INTERESTS_STORAGE_DRIVER env variable.
Database driver
To use the database driver need to publish and run package migration:
php artisan vendor:publish --provider="NotificationChannels\ExpoPushNotifications\ExpoPushNotificationsServiceProvider" --tag="migrations" php artisan migrate
Important
The migration uses the table name defined in interests.database.table_name from the config (exponent_push_notification_interests by default).
Usage
To send an Expo push notification, add ExpoChannel::class to the via() method of your notification class and implement the toExpoPush() method:
use App\Models\User; use NotificationChannels\ExpoPushNotifications\ExpoChannel; use NotificationChannels\ExpoPushNotifications\ExpoMessage; use Illuminate\Notifications\Notification; class AccountApproved extends Notification { public function via(User $notifiable): array { return [ExpoChannel::class]; } public function toExpoPush(User $notifiable): ExpoMessage { return ExpoMessage::create() ->badge(1) ->enableSound() ->title("Congratulations!") ->body("Your {$notifiable->service} account was approved!"); } }
Push tokens API
The package automatically registers the following endpoints for managing device subscriptions:
POST /exponent/devices/subscribe
| Field | Type | Required | Description |
|---|---|---|---|
expo_token |
string | Yes | The Expo push token of the device to subscribe |
POST /exponent/devices/unsubscribe
| Field | Type | Required | Description |
|---|---|---|---|
expo_token |
string | No | The Expo push token to remove. If omitted, all tokens for the authenticated user will be removed |
The middleware applied to these endpoints is configured via the middleware key in the config file.
The package registers all endpoints automatically. To customize their middleware, prefix, or guards, call Route::expo() in your routes file — this disables the automatic registration:
Route::prefix('v{version}')->group(function () { Route::middleware('auth_group')->group(function () { // other routes Route::expo(); }); });
Message customization
ExpoMessage may be customized using the following methods:
| Method | Description | Notes |
|---|---|---|
title |
Sets the notification title | Default: null |
body |
Sets the notification body | Default: '' |
enableSound |
Enables the notification sound | Sets sound to 'default' |
disableSound |
Mutes the notification sound | Sets sound to null |
badge |
Sets the badge count on the app icon | Default: 0 |
setTtl |
Sets the time to live in seconds | Default: 0 |
setJsonData |
Sets additional payload data | Accepts array or JSON string. Throws CouldNotCreateMessage on invalid JSON |
setChannelId |
Sets the notification channel ID | Android only |
priority |
Sets the delivery priority | Default: PriorityEnum::Default.Available: ::Default, ::Normal, ::High |
setInterruptionLevel |
Sets the interruption level | iOS only. Available: InterruptionLevelEnum::Active, ::Critical, ::Passive, ::TimeSensitive |
For example, to send a high-priority time-sensitive notification on iOS:
use NotificationChannels\ExpoPushNotifications\ExpoMessage; use NotificationChannels\ExpoPushNotifications\Enums\PriorityEnum; use NotificationChannels\ExpoPushNotifications\Enums\InterruptionLevelEnum; ExpoMessage::create() ->title('Important!') ->body('This is a critical alert.') ->priority(PriorityEnum::High) ->setInterruptionLevel(InterruptionLevelEnum::TimeSensitive);
Routing a message
An interest is a named subscription that links a device's push token to a logical target (e.g. a specific user). When sending a notification, the package looks up all tokens registered under that interest and delivers the message to them.
By default, the interest that a message is sent to is derived from the notifiable model using the {ClassName}.{id} convention (e.g. App.Models.User.1).
You can override this by adding a routeNotificationForExpoPushNotifications() method to your notifiable model:
use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; public function routeNotificationForExpoPushNotifications(): string { return 'App.Models.User.' . $this->id; } }
Changelog
Please see CHANGELOG for more information on what has changed recently.
Testing
composer test
Security
If you discover any security related issues, please use the GitHub issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
- Aly Suleiman — original author
- RonasIT
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
ronasit/laravel-exponent-push-notifications 适用场景与选型建议
ronasit/laravel-exponent-push-notifications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.5k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ronasit/laravel-exponent-push-notifications 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ronasit/laravel-exponent-push-notifications 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-03