cjpanilag/simple-notifications 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cjpanilag/simple-notifications

最新稳定版本:1.0.1

Composer 安装命令:

composer require cjpanilag/simple-notifications

包简介

Simplified notifications for AWS SNS, FCM Push Notifications, emails, and more.

README 文档

README

Latest Version on Packagist Total Downloads GitHub Repo stars Discord Twitter Follow

Simplified notifications for AWS SNS, FCM Push Notifications, emails, and more.

Installation

Via Composer

$ composer require cjpanilag/simple-notifications --with-all-dependencies
$ php artisan migrate

After migration, simple-notification will generate two (2) tables namely fcm_tokens & simple_devices. Make sure to remove any conflicting table or attribute in your root project.

Usage

AWS SNS

User Model Configuration

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;
    }
}

Note: In order to let your Notification know which phone you are sending to, the channel will look for the phone, phone_number or full_phone attribute of the Notifiable model.

Sending SMS to user

// 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 using content callable

// 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();

Sending SMS using SnsMessage as content

// 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();

Available Methods

method parameter return type description
user Model static assigning user.
content SnsMessage string callable static can be message or a callable function.
mobile string static replacement for user methods.
execute none void will execute the sending.

Note: user or mobile method should not be used at the same time.

Example:

simpleNotifications()->sns()
            ->user($user)               // User already have <mobile number attribute>...
            ->mobile('+63923456789')    // Will conflict with User mobile number attribute...
            ->content('Message here...')
            ->execute();

SES MAIL

User Model Configuration

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;
    }
}

Note: By default, Notification will send email to User's email attribute.

Sending Mail to user

// 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('http://localhost')
    ->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('http://localhost')
    ->execute();

Sending Mail using MailMessage as content

// 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();

Sending Mail using MailMessage with customized view as content

// 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();

Available Methods

method parameter return type description
user Model static notifiable.
subject string static email subject.
title string static email title.
footer string static email footer.
actionMessage string static action message.
actionUrl string static action URL.
emailAddress string static replacement for user methods.
execute none static execute notification.

Note: user or emailAddress method should not be used at the same time.

Example:

$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('http://localhost')
    ->execute();

FCM Notification

Make sure that you already run artisan migrate after installing this package. FCM will use the fcm_tokens table and simple_devices table.

simple_devices table stores users' devices. Each device can have multiple FCM tokens. fcm_tokens table stores the devices' FCM tokens.

Simple Device API

A route will be provided by this package. Check your project level routes/api.php for possible conflicts.

POST /api/device

body

key description
device_id required unique
unique_id required
brand nullable
type nullable
name nullable
manufacturer nullable
model nullable
system_name nullable
system_version nullable
version nullable
active nullable

FCM Token API

A route will be provided by this package. Check your project level routes/api.php for possible conflicts.

POST /api/fcm-token
key description
device_uuid required
token required

Sending FCM Notification to user

$user = User::first();

simpleNotifications()
    ->fcm()
    ->user($user)
    ->data([])
    ->title('Welcome Test')
    ->body('just test')
    ->execute();

Sending FCM Notification using FCM Token

simpleNotifications()
    ->fcm()
    ->token($token)
    ->data([])
    ->title('Welcome Test')
    ->body('just test')
    ->execute();

Available Methods

method parameter description
user Model notifiable
data array notification data
title string notification title
body string notification message
execute none execute send

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email cjpanilag@gmail.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

统计信息

  • 总下载量: 581
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固