weelis/notification

此软件包的最新版本(v1.0.3)没有提供许可信息。

Weelis通知通道(APN、FCM、SMS、EMAIL)

安装: 11

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:weelis-modules

v1.0.3 2023-04-14 14:54 UTC

This package is auto-updated.

Last update: 2024-09-14 18:09:30 UTC


README

Weelis通知通道(APN、FCM、ESMS)

使用此软件包通过Laravel向Firebase Cloud Messaging、APN、ESMS发送推送通知。需要Laravel 5.3+。

安装

此软件包可以通过Composer安装。

composer require weelis/notification

安装后,添加服务提供者

// config/app.php
'providers' => [
    ...
    Weelis\Notification\NotificationServiceProvider::class,
    ...
];
'aliases' => [
    ...
    'NotificationHelper'=> \Weelis\Notification\Facade\NotificationHelper::class
    ...
]

发布配置文件

php artisan vendor:publish --provider="Weelis\Notification\NotificationServiceProvider"

注册设备

使用现有控制器添加到您的路由中

Route::group(['prefix' => 'device'], function () {
    Route::post('register', '\Weelis\Notification\Controller\DevicesController@registerDevice');
    Route::post('unregister', '\Weelis\Notification\Controller\DevicesController@unregisterDevice');
});

使用通知数据库模型和报告

use Weelis\Notification\Base\Notifiable;
use Weelis\Notification\Model\NotificationModel;

class User extends Authenticatable
{
    use Notifiable, NotificationModel;
    ...
}

使用外观

$request => ['os'         => 'required',
//          'device'       => 'required',
			'type'       => 'required',
			'did'        => 'required',
			'scope'      => 'required',
//			'push_token' => 'required']
NotificationHelper::registerDevice($request);

设置FCM服务

以下配置文件将发布在config/notification.php中。在此处添加您的Firebase API密钥。

设置.env文件

FCM_API_KEY=legacy key

或者

return [
    /*
     * Add the Firebase API key
     */
    'fcm' => [
        'api_key' => ''
    ],
];

示例用法

使用Artisan创建通知

php artisan make:notification SomeNotification

在您的通知的public function via($notifiable)方法中返回[fcm]

public function via($notifiable)
{
    return ['fcm'];
}

在您的通知中添加方法public function toFcm($notifiable),并返回一个FcmMessage实例

public function toFcm($notifiable) 
{
    $message = new Weelis\Notification\Fcm\FcmMessage();
    $message->content([
        'title'        => 'Foo', 
        'body'         => 'Bar', 
        'sound'        => '', // Optional 
        'icon'         => '', // Optional
        'click_action' => '' // Optional
    ])->data([
        'param1' => 'baz' // Optional
    ])->priority(Weelis\Notification\Fcm\FcmMessage::PRIORITY_HIGH); // Optional - Default is 'normal'.
    
    return $message;
}

在向特定设备发送时,确保您的通知实体定义了routeNotificationForFcm方法

/**
 * Route notifications for the FCM channel.
 *
 * @return string
 */
public function routeNotificationForFcm()
{
    return $this->device_token;
}

在向一个主题发送时,您可以在通知的toFcm方法中定义

public function toFcm($notifiable) 
{
    $message = new Weelis\Notification\Fcm\FcmMessage();
    $message->to('the-topic', $recipientIsTopic = true)
    ->content([...])
    ->data([...]);
    
    return $message;
}

设置APN服务

在开始使用APN服务之前,请遵循Apple的配置和开发指南

在您可以使用此通道之前,您需要为您的应用程序生成一个证书。在config/broadcasting.php中配置路径

设置.env文件

APN_KEY_DEV={"user":{"cert":"/storage/path/file","pass":"passphrase"},"worker":{"cert":"/storage/path/file","pass":"passphrase"}}
APN_KEY_PRO={"user":{"cert":"/storage/path/file","pass":"passphrase"},"worker":{"cert":"/storage/path/file","pass":"passphrase"}}

用法

您现在可以通过创建一个ApnMessage来向APN发送消息

在您的通知的public function via($notifiable)方法中返回[apn]

use Weelis\Notification\Apn\ApnMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return ['apn'];
    }

    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->badge(1)
            ->title('Account approved')
            ->body("Your {$notifiable->service} account was approved!");
    }
}

在您的通知模型中,确保包含一个routeNotificationForApn方法,该方法返回一个或多个令牌。

public function routeNotificationForApn()
{
    return $this->apn_token;
}

设置ESMS服务

设置.env文件

ESMS_API_KEY=<key>
ESMS_SECRET_KEY=<secrect>
ESMS_SMS_TYPE=6
ESMS_BRAND_NAME=
ESMS_URL=http://rest.esms.vn/MainService.svc/json/SendMultipleMessage_V4_get
ESMS_DAY_MAX=5

用法

您现在可以向ESMS发送消息

在您的通知的public function via($notifiable)方法中返回[esms]

use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return ['esms'];
    }

    public function toApn($notifiable)
    {
        return [
            "sms" => <your sms message>
        ];
    }
}

在向特定设备发送时,确保您的通知实体定义了routeNotificationForEsms方法

/**
 * Route notifications for the FCM channel.
 *
 * @return string
 */
public function routeNotificationForEsms()
{
    return $this->phone;
}

发送用户渠道

use Weelis\Notification\Base\NotificationToUser;

$user->notify(new NotificationToUser([
    'scope' => "user",
    'types' => ['email', 'esms', 'apn', 'fcm'],
    'title' => "Foo",
    'body' => "Bar",
    'icon' => "", // Optional
    'sound' => "", // Optional
    'type' => "Foo Bar",
    'type_slug' => "foo-bar",
    'email_view' => 'foo.bar', // Optional
    'custom' => [ // use for email view param also
        'foo' => '',
        'bar' => '',
        'click_action' => ""
    ] // Optional
]), $notifitable_model  // Optional);

许可

此项目属于vias公司。