elattar/pusher-notification 问题修复 & 功能扩展

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

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

elattar/pusher-notification

最新稳定版本:1.1

Composer 安装命令:

composer require elattar/pusher-notification

包简介

Pusher Notification Package

README 文档

README

  • Install prepare package from HERE
  • Install pusher notification package
composer require elattar/pusher-notification
  • Publish Notification Module
php artisan vendor:publish --tag=elattar-pusher-notifications
  • Enable Notification Module
php artisan elattar:pusher-notification-enable
  • Publish migration file
php artisan notifications:table
php artisan migrate
  • app\Models\User.php
<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function receivesBroadcastNotificationsOn(): string
    {
        return 'notifications.users.' . $this->id;
    }
}
  • config\app.php
<?php

use Illuminate\Support\ServiceProvider;

return [
    'providers' => ServiceProvider::defaultProviders()->merge([
        ... 
        App\Providers\BroadcastServiceProvider::class,
    ])->toArray(),
];
  • app\Providers\BroadcastServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        require_once base_path('Modules/Notification/Routes/channels.php');
    }
}
  • routes\api.php
<?php

use App\Http\Controllers\FileManagerController;
use App\Http\Controllers\SelectMenuController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Broadcast::routes();
// Broadcast::routes(['middleware' => ['auth:sanctum']]); // add auth middleware 

Pusher Configurations

  • .env
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=YOUR_Pusher_ID
PUSHER_APP_KEY=YOUR_PUSHER_KEY
PUSHER_APP_SECRET=YOUR_PUSHER_SECRET
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=YOUR_PUSHER_CLUSTER
PUSHER_HOST=
PUSHER_PORT=443

To test that out try that block of code

<!DOCTYPE html>
<html>

<head>
    <title>Document</title>
</head>
<body>
    <!-- Axios -->
    <script src="https://js.pusher.com/7.2/pusher.min.js"></script>
    <!-- <script src="main.js"></script> -->
    <script>
        // Enable pusher logging - don't include this in production
        Pusher.logToConsole = true;

        const token = "2|FVyVdy317Mp866IP5p0G9JyVv5FiwR3qAGTlnjgL",
                PUBLIC_KEY = 'Your_Public_Key',
                CLUSTER ='mt1',
                AUTH_ENDPOINT = 'https://yourdomain.com/broadcasting/auth',
                CHANNEL_NAME = `private-notifications.users.${LocalStorage.getItem('loggedUserId')}`;
                EVENT_NAME = "Illuminate\\Notifications\\Events\\BroadcastNotificationCreated";
                
        let pusher = new Pusher(PUBLIC_KEY, {
            cluster: CLUSTER,
            channelAuthorization: {
                withCredentials:true,
                endpoint: AUTH_ENDPOINT,
                headers: {
                    "Authorization": `Bearer ${token}`,
                }
            }
        });

        let channel = pusher.subscribe(CHANNEL_NAME);
        channel.bind(EVENT_NAME, function (data) {
            console.log(data);
        });
    </script>
</body>

</html>

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-21