karunais13/multiple-notification-provider

v3.1.1 2024-07-16 01:05 UTC

README

Build Status Latest Stable Version PHP version License

Laravel 有一些非常酷的通知发送功能。由于业务性质,某些 Laravel 辅助函数需要修改。因此,创建了这个包来处理业务性质。

此包允许我们使用第三方服务如 OneSignalFirebase 向所有设备(iOS、Android、Web)发送推送和电子邮件通知。

安装

使用常规 composer 方式安装。

在项目根目录下运行此命令
"composer require karunais13/multiple-notification-provider"

对于 Laravel 5.5 及以下版本,在配置文件中添加提供者,如下所示

app/config/app.php
	...
	
	'providers' => array(
		...
		Karu\NpNotification\NpNotificationProvider::class,
	],
	
	...

        'aliases' => [
            ...
            NotificationHelper: Karu\NpNotification\Facade\NotificationFacade::class
        ]

配置

将包的配置和路由文件复制到相应的文件夹。

 php artisan vendor:publish --provider=Karu\NpNotification\NpNotificationProvider
app/config/notification.php
<?php

return [
    /*
     * The 3rd party service use to send notification .
     * Supported for now : web -> onesignal  email -> default (Will add in more service in feature)
     */
    'service' => [
        'web'    => 'onesignal',
        'email'  => 'default',
        'mobile' => 'onesignal'
    ],

    /*
     * Array contain template for all the notification.
     */
    'template' => [
        /*
         * Unique template code for notification helper to choose form the view folder.
         */
        '{templateCode}' => [
            'web_push'  => [
                'subject' => '', //subject
                'content' => '' //view location Ex : notification.%s.{templateCode}.pic.email_subject (%s -> country_code)
            ],
            'mobile_push' => [
                'subject' => '', //subject
                'content' => '' //view location Ex : notification.%s.{templateCode}.pic.email_subject (%s -> country_code)
            ],
            'email' => [
                'subject' => '', //subject
                'content' => '' //view location Ex : notification.%s.{templateCode}.pic.email_subject (%s -> country_code)
            ],
            'sms'   => [
                'subject' => '', //subject
                'content' => '' //view location Ex : notification.%s.{templateCode}.pic.email_subject (%s -> country_code)
            ]
        ]
    ],

    /*
     * Method used to get user information.
     * This method must be added to respective modal class
     */
    'user_info_method' => 'getNotificationUserInfo',

    /*
     * Table names
     */
    'tables' => [
        'notification_store' => 'notification',
        'notification_token' => 'notification_token',
    ],

    /*
     * User Type
     */
    'user_type' => [
        'd' => \App\Models\Sample::class, // Sample
        'c' => \App\Models\Sample2::class, // Sample
    ],


    /*
     * Store/Log Notification on database
     */
    'log_notification' => true
];
    
app/routes/notification.php
<?php
/*
|--------------------------------------------------------------------------
| Notification
|--------------------------------------------------------------------------
|
*/
Route::group(['prefix'=> 'notification'], function(){
    /*
    |
    | User Class -> set in the notification config with type as key
    |
    */
    Route::put('token/{user_class}/{user_id}', 'NotificationTokenController@update')
        ->name('noti.update-installation');
    Route::put('/{notification_id}', 'NotificationController@update')
        ->name('noti.update');
});

用法

通知类型常量

    NOTIFICATION_TYPE_EMAIL 
    NOTIFICATION_TYPE_WEB_PUSH    
    NOTIFICATION_TYPE_NATIVE_PUSH 
    NOTIFICATION_TYPE_SMS          

设置配置(可选)

 //Default Setting
 $notiSetting = [
    'email' => true,
    'notification' => true, //Web & Mobile
    'sms' => false
 ];

 $noti = NotificationHelper::setConfig($notiSetting)->sendNotificationToUser($user, $templateCode, $extraParam);

发送通知

 $noti = NotificationHelper::sendNotificationToUser($user, $templateCode, $extraParam);

获取通知列表

    /**
     * @param $userId
     * @param $userType (User class name)
     * @param $notiType (NOTIFICATION_TYPE_EMAIL | NOTIFICATION_TYPE_WEB_PUSH | NOTIFICATION_TYPE_NATIVE_PUSH | NOTIFICATION_TYPE_SMS)
     * @param  int  $pastDay (For all record send 0) 
     *
     * @return collection
     */
 $notiList = NotificationHelper::getUnReadUserNotificationList($userId, $userType, $notiType, $pastDay);

取消用户订阅通知

    /**
     * @param $userId
     * @param $userClassType (from notification config user type) 
     * @param $token
     *
     * @return bool
     */
 $notiList = NotificationHelper::unsubscribeUser($userId, $userClassType, $token);

许可证

在此存储库中查看许可证。