cjpanilag / simple-notifications
为AWS SNS、FCM推送通知、电子邮件等功能提供简化通知。
Requires
- php: ^8.0
- illuminate/support: ~8|~9|~10
- kreait/firebase-php: 6.3.1
- laravel-notification-channels/aws-sns: ^1.4
- laravel-notification-channels/fcm: ~2.0
- luchavez/aws-ses-bounce: ^1.0
- luchavez/starter-kit: ^1.0
Requires (Dev)
- orchestra/testbench: ~6|~7|~8
- phpunit/phpunit: ~9.0
- roave/security-advisories: dev-latest
Suggests
This package is auto-updated.
Last update: 2024-09-30 01:59:58 UTC
README
为AWS SNS、FCM推送通知、电子邮件等功能提供简化通知。
安装
通过Composer安装
$ composer require cjpanilag/simple-notifications --with-all-dependencies $ php artisan migrate
迁移后,simple-notification
将生成两个表,即fcm_tokens
和simple_devices
。请确保删除根项目中任何冲突的表或属性。
用法
AWS SNS
用户模型配置
use Cjpanilag\SimpleNotifications\Traits\HasSnsNotifiableTrait; class User extends Authenticatable { use HasApiTokens, HasFactory, HasSnsNotifiableTrait; /** * Overridden method (optional) * By default, `SnsNoticiableTrait` will look * for the specific attribute in your model: * `phone`, * `phone_number`, * `full_phone` * * @override * @param $notification * @return string|null */ public function routeNotificationForSns($notification): string|null { // model attribute return $this->specific_mobile_number_attribute; } }
注意:为了让你的Notification
知道你正在向哪个电话发送,通道将查找Notifiable
模型中的phone
、phone_number
或full_phone
属性。
向用户发送短信
// Sending SMS to a User model... $user = User::first(); simpleNotifications()->sns() ->user($user) ->content('Message here...') ->execute(); // Sending SMS to a specific phone... simpleNotifications()->sns() ->mobile('+639123456789') ->content('Message here...') ->execute();
使用内容可调用函数发送短信
// Sending SMS to a User model... $user = User::first(); simpleNotifications()->sns() ->user($user) ->content(fn($user) => "Welcome $user->name") // passing notifiable ->execute(); // Sending SMS to a specific phone... simpleNotifications()->sns() ->mobile('+639123456789') ->content(fn($user) => "Welcome $user->name") // passing notifiable ->execute();
使用SnsMessage
作为内容发送短信
// Sending SMS to a User model... $user = User::first(); simpleNotifications()->sns()->user($user)->content(function ($user) { $snsMessage = new SnsMessage('message here...'); $snsMessage->sender('FIN-PAY'); $snsMessage->transactional(true); return $snsMessage; })->execute(); // Sending SMS to a specific phone... simpleNotifications()->sns()->mobile('+639123456789')->content(function ($user) { $snsMessage = new SnsMessage('message here...'); $snsMessage->sender('FIN-PAY'); $snsMessage->transactional(true); return $snsMessage; })->execute();
可用方法
注意:user
或mobile
方法不应同时使用。
示例
simpleNotifications()->sns() ->user($user) // User already have <mobile number attribute>... ->mobile('+63923456789') // Will conflict with User mobile number attribute... ->content('Message here...') ->execute();
SES 邮件
用户模型配置
use Cjpanilag\SimpleNotifications\Traits\HasMailNotifiableTrait; class Model extends Authenticatable { use HasApiTokens, HasFactory, HasMailNotifiableTrait; /** * @param $notification * @return array|string|null */ public function routeNotificationForMail($notification): array|string|null { // Define a specific <email attribute> return $this->email_address; } }
注意:默认情况下,Notification
会向用户的email
属性发送电子邮件。
向用户发送邮件
// Sending mail to a User model... $user = User::first(); simpleNotifications()->mail() ->user($user) ->subject('Test Subject 123') ->body('test body') ->footer('test footer') ->actionMessage('PUSH ME!') ->actionUrl('https://') ->execute(); // Sending mail to a specific email address... simpleNotifications()->mail() ->emailAddress('testemail123@gmail.com') ->subject('Test Subject 123') ->body('test body') ->footer('test footer') ->actionMessage('PUSH ME!') ->actionUrl('https://') ->execute();
使用MailMessage
作为内容发送邮件
// Sending mail to a User model... $user = User::first(); simpleNotifications()->mail()->user($user)->content(function ($user) { $mailMessage = new MailMessage(); $subject = 'test subject 2'; if ($user) { $mailMessage->subject($subject); } $mailMessage->line('this is a best body number 2') ->action('PUSH ME!', 'https://test.com') ->line('this is a test footer 2'); return $mailMessage; })->execute(); // Sending mail to a specific email address... ```php simpleNotifications()->mail()->emailAddress('testemail123@gmail.com')->content(function ($user) { $mailMessage = new MailMessage(); $subject = 'test subject 2'; if ($user) { $mailMessage->subject($subject); } $mailMessage->line('this is a best body number 2') ->action('PUSH ME!', 'https://test.com') ->line('this is a test footer 2'); return $mailMessage; })->execute();
使用自定义view
的MailMessage
作为内容发送邮件
// Sending mail to a specific email address... simpleNotifications()->mail()->emailAddress('testemail123@gmail.com')->content(function ($user) { $mailMessage = new MailMessage(); $mailMessage->view('view.template'); return $mailMessage; })->execute();
可用方法
注意:user
或emailAddress
方法不应同时使用。
示例
$user = User::first(); simpleNotifications()->mail() ->user($user) // user has email attribute ->emailAddress('testemail123@gmail.com') // conflict with email attribute ->subject('Test Subject 123') ->body('test body') ->footer('test footer') ->actionMessage('PUSH ME!') ->actionUrl('https://') ->execute();
FCM通知
确保安装此包后已经运行了artisan migrate
。FCM将使用fcm_tokens
表和simple_devices
表。
simple_devices
表存储用户的设备。每个设备可以有多个FCM令牌。fcm_tokens
表存储设备的FCM令牌。
简单设备API
此包将提供一个路由。检查你的项目级别routes/api.php
以避免冲突。
POST /api/device
body
FCM令牌API
此包将提供一个路由。检查你的项目级别routes/api.php
以避免冲突。
POST /api/fcm-token
向用户发送FCM通知
$user = User::first(); simpleNotifications() ->fcm() ->user($user) ->data([]) ->title('Welcome Test') ->body('just test') ->execute();
使用FCM令牌发送FCM通知
simpleNotifications() ->fcm() ->token($token) ->data([]) ->title('Welcome Test') ->body('just test') ->execute();
可用方法
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
测试
$ composer test
贡献
请参阅contributing.md以获取详细信息和工作列表。
安全
如果您发现任何安全相关的问题,请通过电子邮件cjpanilag@gmail.com报告,而不是使用问题跟踪器。
致谢
许可证
MIT。请参阅许可证文件以获取更多信息。