Làm cách nào để phát thông báo trong laravel?

Tôi đang cố gắng sử dụng hệ thống Pusher Notificaiton để sử dụng ổ cắm web để cập nhật trang hiện tại với thông báo "Bạn có Thông báo mới" và biểu tượng chuông cho biết số lượng thông báo chưa đọc. Tôi đã đăng ký Sự kiện và Người nghe và đã triển khai

id));   
    }
}

67 và kết nối mọi thứ. Nhưng tôi không nhận được bất kỳ tin nhắn. Và trừ khi trang tải lại, tôi sẽ không nhận được thông báo không. của các thông báo chưa đọc là tốt

Đây là mã của tôi

Biến cố

data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return ['my-channel'];
    }
    public function broadcastAs()
    {
      return 'my-event';
    }
}

Thính giả

id));   
    }
}

Bộ điều khiển

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}

Thông báo được gửi từ Backend (Trình điều khiển phụ trợ)

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }

Mã JS (Có sẵn trong trang lưỡi phổ biến - trang bao gồm Thanh điều hướng và được bao gồm trong mọi trang khác)

Trong nhiều ứng dụng web hiện đại, WebSockets được sử dụng để triển khai giao diện người dùng cập nhật trực tiếp theo thời gian thực. Khi một số dữ liệu được cập nhật trên máy chủ, một thông báo thường được gửi qua kết nối WebSocket để máy khách xử lý. Điều này cung cấp một giải pháp thay thế mạnh mẽ, hiệu quả hơn để liên tục bỏ phiếu cho ứng dụng của bạn để biết các thay đổi

Để hỗ trợ bạn xây dựng các loại ứng dụng này, Laravel giúp bạn dễ dàng "truyền phát" các sự kiện của mình qua kết nối WebSocket. Phát các sự kiện Laravel của bạn cho phép bạn chia sẻ các tên sự kiện giống nhau giữa mã phía máy chủ và ứng dụng JavaScript phía máy khách của bạn

{tip} Trước khi đi sâu vào phát sóng sự kiện, hãy đảm bảo rằng bạn đã đọc tất cả tài liệu về các sự kiện và trình lắng nghe của Laravel

Cấu hình

Tất cả cấu hình phát sóng sự kiện của ứng dụng của bạn được lưu trữ trong tệp cấu hình

composer require predis/predis

9. Laravel hỗ trợ một số trình điều khiển phát sóng ngay lập tức. Pusher Channels, Redis và trình điều khiển

0 để phát triển và gỡ lỗi cục bộ. Ngoài ra, trình điều khiển

1 được bao gồm cho phép bạn tắt hoàn toàn tính năng phát sóng. Một ví dụ cấu hình được bao gồm cho từng trình điều khiển này trong tệp cấu hình

composer require predis/predis

9

Nhà cung cấp dịch vụ phát sóng

Trước khi phát bất kỳ sự kiện nào, trước tiên bạn cần đăng ký

3. Trong các ứng dụng Laravel mới, bạn chỉ cần bỏ ghi chú trình cung cấp này trong mảng

4 của tệp cấu hình

5 của bạn. Nhà cung cấp này sẽ cho phép bạn đăng ký các tuyến ủy quyền quảng bá và gọi lại

Mã thông báo CSRF

Laravel Echo sẽ cần quyền truy cập vào mã thông báo CSRF của phiên hiện tại. Bạn nên xác minh rằng phần tử HTML

6 của ứng dụng của bạn xác định thẻ

7 chứa mã thông báo CSRF

id));   
    }
}

3

Điều kiện tiên quyết của trình điều khiển

Kênh đẩy

Nếu bạn đang phát các sự kiện của mình trên Kênh Pusher, bạn nên cài đặt SDK PHP Kênh Pusher bằng trình quản lý gói Composer

id));   
    }
}

4

Tiếp theo, bạn nên định cấu hình thông tin đăng nhập Kênh của mình trong tệp cấu hình

composer require predis/predis

9. Một ví dụ về cấu hình Kênh đã được bao gồm trong tệp này, cho phép bạn nhanh chóng chỉ định khóa Kênh, bí mật và ID ứng dụng của mình. Cấu hình
id));   
    }
}

70 của tệp

composer require predis/predis

9 cũng cho phép bạn chỉ định thêm
id));   
    }
}

71 được Kênh hỗ trợ, chẳng hạn như cụm

Khi sử dụng Kênh và Laravel Echo, bạn nên chỉ định

id));   
    }
}

70 làm trình phát mong muốn của mình khi khởi tạo phiên bản Echo trong tệp
id));   
    }
}

73 của bạn

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
1

làm lại

Nếu bạn đang sử dụng Redis Broadcaster, bạn nên cài đặt thư viện Predis

composer require predis/predis

Redis Broadcaster sẽ phát tin nhắn bằng tính năng pub/sub của Redis;

Khi đài truyền hình Redis xuất bản một sự kiện, nó sẽ được xuất bản trên các tên kênh được chỉ định của sự kiện và tải trọng sẽ là một chuỗi được mã hóa JSON chứa tên sự kiện, tải trọng

id));   
    }
}

74 và người dùng đã tạo ID ổ cắm của sự kiện (nếu có)

Ổ cắm. IO

Nếu bạn định ghép nối Redis Broadcaster với một Socket. Máy chủ IO, bạn sẽ cần bao gồm Ổ cắm. Thư viện máy khách IO JavaScript trong phần tử HTML

6 của ứng dụng của bạn. Khi ổ cắm. Máy chủ IO được khởi động, nó sẽ tự động hiển thị thư viện JavaScript của máy khách tại một URL tiêu chuẩn. Ví dụ, nếu bạn đang chạy Socket. Máy chủ IO trên cùng miền với ứng dụng web của bạn, bạn có thể truy cập thư viện máy khách như vậy

Tiếp theo, bạn sẽ cần khởi tạo Echo bằng trình kết nối

id));   
    }
}

76 và một
id));   
    }
}

77

id));   
    }
}

7

Cuối cùng, bạn sẽ cần chạy một Ổ cắm tương thích. máy chủ IO. Laravel không bao gồm Socket. Triển khai máy chủ IO; . Máy chủ IO hiện được duy trì tại kho lưu trữ GitHub của talverdure/laravel-echo-server

Điều kiện tiên quyết của hàng đợi

Trước khi phát sự kiện, bạn cũng sẽ cần định cấu hình và chạy trình nghe hàng đợi. Tất cả các sự kiện phát sóng được thực hiện thông qua các công việc được xếp hàng để thời gian phản hồi của ứng dụng của bạn không bị ảnh hưởng nghiêm trọng

Khái niệm tổng quan

Phát sự kiện Laravel cho phép bạn phát các sự kiện Laravel phía máy chủ tới ứng dụng JavaScript phía máy khách của bạn bằng cách sử dụng phương pháp dựa trên trình điều khiển cho WebSockets. Hiện tại, Laravel cung cấp trình điều khiển Pusher Channels và Redis. Các sự kiện có thể dễ dàng được sử dụng ở phía máy khách bằng cách sử dụng gói Laravel Echo Javascript

Các sự kiện được phát trên "kênh", có thể được chỉ định là công khai hoặc riêng tư. Bất kỳ khách truy cập nào vào ứng dụng của bạn đều có thể đăng ký kênh công khai mà không cần bất kỳ xác thực hoặc ủy quyền nào;

Sử dụng một ứng dụng ví dụ

Trước khi đi sâu vào từng thành phần của phát sóng sự kiện, hãy xem tổng quan cấp cao bằng cách sử dụng cửa hàng thương mại điện tử làm ví dụ. Chúng tôi sẽ không thảo luận chi tiết về cấu hình Pusher Channels hoặc Laravel Echo vì điều đó sẽ được thảo luận chi tiết trong các phần khác của tài liệu này

Trong ứng dụng của chúng ta, giả sử chúng ta có một trang cho phép người dùng xem trạng thái vận chuyển cho đơn hàng của họ. Cũng giả sử rằng một sự kiện

id));   
    }
}

78 được kích hoạt khi ứng dụng xử lý cập nhật trạng thái vận chuyển

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
0

Giao diện
id));   
    }
}

79

Khi người dùng đang xem một trong các đơn đặt hàng của họ, chúng tôi không muốn họ phải làm mới trang để xem cập nhật trạng thái. Thay vào đó, chúng tôi muốn phát các bản cập nhật cho ứng dụng khi chúng được tạo. Vì vậy, chúng ta cần đánh dấu sự kiện

id));   
    }
}

78 bằng giao diện
id));   
    }
}

79. Điều này sẽ hướng dẫn Laravel phát sự kiện khi nó được kích hoạt

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
4

Giao diện

id));   
    }
}

79 yêu cầu sự kiện của chúng tôi xác định phương thức
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
03. Phương thức này chịu trách nhiệm trả lại các kênh mà sự kiện sẽ phát trên. Sơ khai trống của phương thức này đã được xác định trên các lớp sự kiện được tạo, vì vậy chúng ta chỉ cần điền thông tin chi tiết của nó. Chúng tôi chỉ muốn người tạo đơn đặt hàng có thể xem các cập nhật trạng thái, vì vậy chúng tôi sẽ phát sự kiện trên một kênh riêng được liên kết với đơn đặt hàng

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
7

Ủy quyền kênh

Hãy nhớ rằng, người dùng phải được phép nghe trên các kênh riêng tư. Chúng tôi có thể xác định quy tắc ủy quyền kênh của mình trong tệp

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
04. Trong ví dụ này, chúng tôi cần xác minh rằng bất kỳ người dùng nào cố gắng nghe trên kênh
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
05 riêng thực sự là người tạo đơn đặt hàng

composer require predis/predis

0

Phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
06 chấp nhận hai đối số. tên của kênh và lệnh gọi lại trả về
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
07 hoặc
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
08 cho biết liệu người dùng có được phép nghe trên kênh hay không

Tất cả các cuộc gọi lại ủy quyền nhận người dùng hiện được xác thực làm đối số đầu tiên của họ và bất kỳ tham số ký tự đại diện bổ sung nào làm đối số tiếp theo của họ. Trong ví dụ này, chúng tôi đang sử dụng trình giữ chỗ

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
09 để chỉ ra rằng phần "ID" của tên kênh là ký tự đại diện

Lắng nghe các chương trình phát sóng sự kiện

Tiếp theo, tất cả những gì còn lại là lắng nghe sự kiện trong ứng dụng JavaScript của chúng tôi. Chúng ta có thể làm điều này bằng cách sử dụng Laravel Echo. Đầu tiên, chúng ta sẽ sử dụng phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
40 để đăng ký kênh riêng tư. Sau đó, chúng tôi có thể sử dụng phương pháp
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
41 để lắng nghe sự kiện
id));   
    }
}

78. Theo mặc định, tất cả các thuộc tính công khai của sự kiện sẽ được đưa vào sự kiện phát sóng

id));   
    }
}

40

Xác định sự kiện phát sóng

Để thông báo cho Laravel rằng một sự kiện nhất định sẽ được phát, hãy triển khai giao diện

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
43 trên lớp sự kiện. Giao diện này đã được nhập vào tất cả các lớp sự kiện do khung tạo ra, do đó bạn có thể dễ dàng thêm nó vào bất kỳ sự kiện nào của mình

Giao diện

id));   
    }
}

79 yêu cầu bạn triển khai một phương thức duy nhất.
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
03. Phương thức
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
03 sẽ trả về một kênh hoặc mảng kênh mà sự kiện sẽ phát trên đó. Các kênh phải là phiên bản của
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
47,
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
48 hoặc
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
49. Trường hợp của
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
47 đại diện cho các kênh công khai mà bất kỳ người dùng nào cũng có thể đăng ký, trong khi
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
71 và
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
72 đại diện cho các kênh riêng tư yêu cầu ủy quyền kênh

id));   
    }
}

41

Sau đó, bạn chỉ cần kích hoạt sự kiện như bình thường. Khi sự kiện đã được kích hoạt, một công việc được xếp hàng đợi sẽ tự động phát sự kiện qua trình điều khiển phát được chỉ định của bạn

Tên phát sóng

Theo mặc định, Laravel sẽ phát sự kiện bằng tên lớp của sự kiện. Tuy nhiên, bạn có thể tùy chỉnh tên quảng bá bằng cách xác định phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
73 trong sự kiện

id));   
    }
}

42

Nếu bạn tùy chỉnh tên quảng bá bằng phương pháp

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
73, bạn nên đảm bảo đăng ký người nghe của mình bằng ký tự
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
75 ở đầu. Điều này sẽ hướng dẫn Echo không thêm không gian tên của ứng dụng vào sự kiện

id));   
    }
}

43

Dữ liệu phát sóng

Khi một sự kiện được phát, tất cả các thuộc tính

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
76 của nó sẽ tự động được sắp xếp theo thứ tự và phát dưới dạng tải trọng của sự kiện, cho phép bạn truy cập bất kỳ dữ liệu công khai nào từ ứng dụng JavaScript của mình. Vì vậy, ví dụ: nếu sự kiện của bạn có một thuộc tính
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
77 công khai duy nhất chứa mô hình Eloquent, tải trọng quảng bá của sự kiện sẽ là

Tuy nhiên, nếu bạn muốn kiểm soát chi tiết hơn đối với tải trọng phát sóng của mình, bạn có thể thêm phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
78 vào sự kiện của mình. Phương thức này sẽ trả về mảng dữ liệu mà bạn muốn phát dưới dạng tải trọng sự kiện

id));   
    }
}

44

Hàng đợi phát sóng

Theo mặc định, mỗi sự kiện phát sóng được đặt trên hàng đợi mặc định cho kết nối hàng đợi mặc định được chỉ định trong tệp cấu hình

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
79 của bạn. Bạn có thể tùy chỉnh hàng đợi được đài truyền hình sử dụng bằng cách xác định thuộc tính

composer require predis/predis

00 trên lớp sự kiện của bạn. Thuộc tính này phải chỉ định tên của hàng đợi bạn muốn sử dụng khi phát sóng

id));   
    }
}

45

Nếu bạn muốn phát sự kiện của mình bằng cách sử dụng hàng đợi

composer require predis/predis

01 thay vì trình điều khiển hàng đợi mặc định, bạn có thể triển khai giao diện

composer require predis/predis

02 thay vì
id));   
    }
}

79

id));   
    }
}

46

Điều kiện phát sóng

Đôi khi bạn chỉ muốn phát sự kiện của mình nếu một điều kiện nhất định là đúng. Bạn có thể xác định các điều kiện này bằng cách thêm phương thức

composer require predis/predis

04 vào lớp sự kiện của mình

id));   
    }
}

47

Ủy quyền kênh

Các kênh riêng tư yêu cầu bạn cho phép rằng người dùng hiện được xác thực có thể thực sự nghe trên kênh. Điều này được thực hiện bằng cách tạo một yêu cầu HTTP cho ứng dụng Laravel của bạn với tên kênh và cho phép ứng dụng của bạn xác định xem người dùng có thể nghe trên kênh đó không. Khi sử dụng Laravel Echo, yêu cầu HTTP cấp quyền đăng ký cho các kênh riêng tư sẽ được thực hiện tự động;

Xác định các tuyến ủy quyền

Rất may, Laravel giúp dễ dàng xác định các tuyến để đáp ứng các yêu cầu ủy quyền kênh. Trong

composer require predis/predis

05 đi kèm với ứng dụng Laravel của bạn, bạn sẽ thấy lệnh gọi phương thức

composer require predis/predis

06. Phương pháp này sẽ đăng ký tuyến đường

composer require predis/predis

07 để xử lý các yêu cầu ủy quyền

Phương thức

composer require predis/predis

06 sẽ tự động đặt các tuyến của nó trong nhóm phần mềm trung gian

composer require predis/predis

09;

id));   
    }
}

48

Xác định cuộc gọi lại ủy quyền

Tiếp theo, chúng ta cần xác định logic sẽ thực sự thực hiện ủy quyền kênh. Điều này được thực hiện trong tệp

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
04 đi kèm với ứng dụng của bạn. Trong tệp này, bạn có thể sử dụng phương pháp
id));   
    }
}

401 để đăng ký gọi lại ủy quyền kênh

composer require predis/predis

0

Phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
06 chấp nhận hai đối số. tên của kênh và lệnh gọi lại trả về
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
07 hoặc
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
08 cho biết liệu người dùng có được phép nghe trên kênh hay không

Tất cả các cuộc gọi lại ủy quyền nhận người dùng hiện được xác thực làm đối số đầu tiên của họ và bất kỳ tham số ký tự đại diện bổ sung nào làm đối số tiếp theo của họ. Trong ví dụ này, chúng tôi đang sử dụng trình giữ chỗ

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
09 để chỉ ra rằng phần "ID" của tên kênh là ký tự đại diện

Ràng buộc mô hình gọi lại ủy quyền

Giống như các tuyến HTTP, các tuyến kênh cũng có thể tận dụng liên kết mô hình tuyến ẩn và rõ ràng. Ví dụ: thay vì nhận ID đơn đặt hàng dạng chuỗi hoặc số, bạn có thể yêu cầu một phiên bản mô hình

id));   
    }
}

406 thực tế

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
10

sự kiện phát sóng

Khi bạn đã xác định một sự kiện và đánh dấu nó bằng giao diện

id));   
    }
}

79, bạn chỉ cần kích hoạt sự kiện bằng hàm
id));   
    }
}

408. Người điều phối sự kiện sẽ nhận thấy rằng sự kiện được đánh dấu bằng giao diện
id));   
    }
}

79 và sẽ xếp hàng sự kiện để phát sóng

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
0

Chỉ dành cho người khác

Khi xây dựng một ứng dụng sử dụng phát sóng sự kiện, bạn có thể thay thế hàm

id));   
    }
}

408 bằng hàm
id));   
    }
}

411. Giống như chức năng
id));   
    }
}

408, chức năng
id));   
    }
}

411 gửi sự kiện đến người nghe phía máy chủ của bạn

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
12

Tuy nhiên, hàm

id));   
    }
}

411 cũng hiển thị phương thức
id));   
    }
}

415 cho phép bạn loại trừ người dùng hiện tại khỏi người nhận của quảng bá

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
13

Để hiểu rõ hơn khi nào bạn có thể muốn sử dụng phương pháp

id));   
    }
}

415, hãy tưởng tượng một ứng dụng danh sách nhiệm vụ nơi người dùng có thể tạo một nhiệm vụ mới bằng cách nhập tên nhiệm vụ. Để tạo một tác vụ, ứng dụng của bạn có thể đưa ra yêu cầu tới một điểm cuối
id));   
    }
}

417, điểm cuối này sẽ phát sóng quá trình tạo tác vụ và trả về một biểu diễn JSON của tác vụ mới. Khi ứng dụng JavaScript của bạn nhận được phản hồi từ điểm cuối, nó có thể trực tiếp chèn tác vụ mới vào danh sách tác vụ của nó như vậy

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
14

Tuy nhiên, hãy nhớ rằng chúng tôi cũng phát sóng việc tạo nhiệm vụ. Nếu ứng dụng JavaScript của bạn đang lắng nghe sự kiện này để thêm tác vụ vào danh sách tác vụ, thì bạn sẽ có các tác vụ trùng lặp trong danh sách của mình. một từ điểm cuối và một từ chương trình phát sóng

Bạn có thể giải quyết vấn đề này bằng cách sử dụng phương pháp

id));   
    }
}

415 để hướng dẫn đài truyền hình không phát sự kiện cho người dùng hiện tại

Cấu hình

Khi bạn khởi tạo một Laravel Echo instance, một socket ID sẽ được gán cho kết nối. Nếu bạn đang sử dụng Vue và Axios, ID ổ cắm sẽ tự động được đính kèm vào mọi yêu cầu gửi đi dưới dạng tiêu đề

id));   
    }
}

419. Sau đó, khi bạn gọi phương thức
id));   
    }
}

415, Laravel sẽ trích xuất ID ổ cắm từ tiêu đề và hướng dẫn đài truyền hình không phát tới bất kỳ kết nối nào có ID ổ cắm đó

Nếu bạn không sử dụng Vue và Axios, bạn sẽ cần định cấu hình thủ công ứng dụng JavaScript của mình để gửi tiêu đề

id));   
    }
}

419. Bạn có thể truy xuất ID ổ cắm bằng phương pháp
id));   
    }
}

422

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
15

Nhận chương trình phát sóng

Cài đặt Laravel Echo

Laravel Echo là một thư viện JavaScript giúp bạn dễ dàng đăng ký kênh và lắng nghe các sự kiện do Laravel phát sóng. Bạn có thể cài đặt Echo thông qua trình quản lý gói NPM. Trong ví dụ này, chúng tôi cũng sẽ cài đặt gói

id));   
    }
}

423 vì chúng tôi sẽ sử dụng trình phát Kênh Pusher

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
16

Sau khi cài đặt Echo, bạn đã sẵn sàng tạo một phiên bản Echo mới trong JavaScript của ứng dụng của mình. Một nơi tuyệt vời để làm điều này là ở dưới cùng của tệp

id));   
    }
}

73 được bao gồm trong Laravel framework

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
17

Khi tạo một phiên bản Echo sử dụng trình kết nối

id));   
    }
}

70, bạn cũng có thể chỉ định một
id));   
    }
}

426 cũng như liệu kết nối có phải được thực hiện qua TLS hay không (theo mặc định, khi
id));   
    }
}

427 là
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
08, một kết nối không phải TLS sẽ được thực hiện nếu trang được tải

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
18

Lắng nghe sự kiện

Khi bạn đã cài đặt và khởi tạo Echo, bạn đã sẵn sàng bắt đầu nghe các chương trình phát sóng sự kiện. Đầu tiên, sử dụng phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
06 để truy xuất một thể hiện của kênh, sau đó gọi phương thức
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
41 để lắng nghe một sự kiện đã chỉ định

 if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
          
       );

       event (new NewNotificationEvent($data));
       // DB::select('CALL notification_tbl(?)',array($userid));
       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);

       
     }
         abort(403, "This Page is only available for Admin");
    }
}
19

Nếu bạn muốn nghe các sự kiện trên một kênh riêng tư, hãy sử dụng phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
40 để thay thế. Bạn có thể tiếp tục gọi chuỗi theo phương pháp
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
41 để lắng nghe nhiều sự kiện trên một kênh

Bỏ Một Kênh

Để rời khỏi một kênh, bạn có thể gọi phương thức

id));   
    }
}

433 trên phiên bản Echo của mình

không gian tên

Bạn có thể nhận thấy trong các ví dụ trên rằng chúng tôi đã không chỉ định không gian tên đầy đủ cho các lớp sự kiện. Điều này là do Echo sẽ tự động cho rằng các sự kiện nằm trong không gian tên

id));   
    }
}

434. Tuy nhiên, bạn có thể định cấu hình không gian tên gốc khi khởi tạo Echo bằng cách chuyển tùy chọn cấu hình
id));   
    }
}

435

composer require predis/predis

0

Ngoài ra, bạn có thể đặt trước các lớp sự kiện bằng

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
75 khi đăng ký chúng bằng Echo. Điều này sẽ cho phép bạn luôn chỉ định tên lớp đủ điều kiện

composer require predis/predis

1

Kênh hiện diện

Các kênh hiện diện xây dựng dựa trên tính bảo mật của các kênh riêng tư đồng thời hiển thị tính năng bổ sung giúp nhận biết ai đã đăng ký kênh. Điều này giúp dễ dàng xây dựng các tính năng ứng dụng cộng tác mạnh mẽ, chẳng hạn như thông báo cho người dùng khi một người dùng khác đang xem cùng một trang

Cho phép các kênh hiện diện

Tất cả các kênh hiện diện cũng là các kênh riêng tư; . Tuy nhiên, khi xác định các cuộc gọi lại ủy quyền cho các kênh hiện diện, bạn sẽ không trả về

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
07 nếu người dùng được ủy quyền tham gia kênh. Thay vào đó, bạn nên trả về một mảng dữ liệu về người dùng

Dữ liệu được trả về bởi cuộc gọi lại ủy quyền sẽ được cung cấp cho người nghe sự kiện kênh hiện diện trong ứng dụng JavaScript của bạn. Nếu người dùng không được phép tham gia kênh hiện diện, bạn nên quay lại

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
08 hoặc

1

composer require predis/predis

2

Tham gia các kênh hiện diện

Để tham gia kênh hiện diện, bạn có thể sử dụng phương pháp

id));   
    }
}

440 của Echo. Phương thức
id));   
    }
}

440 sẽ trả về một triển khai
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
49, cùng với việc hiển thị phương thức
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
41, cho phép bạn đăng ký các sự kiện
id));   
    }
}

444,
id));   
    }
}

445 và
id));   
    }
}

446

composer require predis/predis

3

Cuộc gọi lại

id));   
    }
}

444 sẽ được thực thi ngay lập tức sau khi kênh được tham gia thành công và sẽ nhận được một mảng chứa thông tin người dùng cho tất cả những người dùng khác hiện đã đăng ký kênh. Phương thức
id));   
    }
}

445 sẽ được thực thi khi người dùng mới tham gia kênh, trong khi phương thức
id));   
    }
}

446 sẽ được thực thi khi người dùng rời khỏi kênh

Phát sóng đến các kênh hiện diện

Các kênh hiện diện có thể nhận các sự kiện giống như các kênh công khai hoặc riêng tư. Sử dụng ví dụ về phòng trò chuyện, chúng tôi có thể muốn phát các sự kiện

id));   
    }
}

450 tới kênh hiện diện của phòng. Để làm như vậy, chúng tôi sẽ trả về một thể hiện của
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
49 từ phương thức
 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
03 của sự kiện

composer require predis/predis

4

Giống như các sự kiện công khai hoặc riêng tư, các sự kiện kênh hiện diện có thể được phát bằng chức năng

id));   
    }
}

411. Cũng như các sự kiện khác, bạn có thể sử dụng phương pháp
id));   
    }
}

415 để loại trừ người dùng hiện tại khỏi việc nhận quảng bá

composer require predis/predis

5

Bạn có thể lắng nghe sự kiện tham gia thông qua phương thức

 public function sendNotf(Request $request){
     if(Gate::allows('manage-users')){
      $userid = $request->userid;
      $notf = $request->notf;
      $priority = $request->priority;

       DB::table('notifications')->insert(['user_id'=>$userid,'notf_message'=>$notf,'priority'=>$priority]);
       DB::select('CALL notification_tbl(?)',array($userid));
        $data = array(
        'userid'=>$userid,
        'priority'=>$priority,
        'notf_message'=>$notf,
        );

       // event (new NewNotificationEvent($data));
       

       return response()->json([
           'success'=>true,
           'msg'=>"User Notified",
       ]);
    }
         abort(403, "This Page is only available for Admin");
    }
41 của Echo

composer require predis/predis

6

Sự kiện khách hàng

Đôi khi bạn có thể muốn phát một sự kiện tới các máy khách được kết nối khác mà không cần nhấn vào ứng dụng Laravel của bạn. Điều này có thể đặc biệt hữu ích cho những thứ như thông báo "đang nhập", nơi bạn muốn thông báo cho người dùng ứng dụng của mình rằng một người dùng khác đang nhập tin nhắn trên một màn hình nhất định. Để phát các sự kiện của khách hàng, bạn có thể sử dụng phương thức

id));   
    }
}

456 của Echo

Để lắng nghe các sự kiện của khách hàng, bạn có thể sử dụng phương pháp

id));   
    }
}

457

composer require predis/predis

7

thông báo

Bằng cách ghép nối phát sự kiện với thông báo, ứng dụng JavaScript của bạn có thể nhận được thông báo mới khi chúng xuất hiện mà không cần làm mới trang. Trước tiên, hãy nhớ đọc qua tài liệu về cách sử dụng kênh thông báo quảng bá

Khi bạn đã định cấu hình thông báo để sử dụng kênh phát sóng, bạn có thể lắng nghe các sự kiện phát sóng bằng phương pháp

id));   
    }
}

458 của Echo. Hãy nhớ rằng tên kênh phải khớp với tên lớp của thực thể nhận thông báo

composer require predis/predis

8

Trong ví dụ này, tất cả các thông báo được gửi tới các phiên bản

id));   
    }
}

459 qua kênh
id));   
    }
}

411 sẽ được gọi lại. Gọi lại ủy quyền kênh cho kênh
id));   
    }
}

461 được bao gồm trong

composer require predis/predis

05 mặc định đi kèm với khung Laravel

Làm cách nào để gửi thông báo SMS trong Laravel?

Bước 1 – Tạo dự án Laravel. .
Bước 2 – Thiết lập Gói SMS Vonage (Nexmo). .
Bước 3 – Tạo và cấu hình bộ điều khiển. .
Bước 4 – Thiết lập lộ trình. .
Bước 5 – Chạy ứng dụng Laravel

Làm cách nào để gửi thông báo đẩy trong Laravel 8?

Hãy bắt đầu với một số bước cơ bản. .
Bước 1. Tạo một dự án Laravel. .
Bước 2. Cấu hình chi tiết cơ sở dữ liệu. .
Bước 3. Tạo Auth bằng giàn giáo. .
Bước 4. Tạo tệp di chuyển để thêm cột. .
Bước 5. Tạo dự án Firebase. .
Bước 6. Thêm bộ điều khiển định tuyến và cập nhật. .
Bước 7. Thêm mã căn cứ hỏa lực. .
Bước 8. Gửi thông báo đẩy

Làm cách nào để gửi thông báo cho nhiều người dùng trong Laravel?

sử dụng Chiếu sáng\Hỗ trợ\Mặt tiền\Thông báo; . ở đâu(. )-> lấy(); . send($users, nhiệm vụ mới được giao($task)); . Thông báo hỗ trợ hàng đợi theo mặc định

Làm cách nào để tạo thông báo đẩy web trong Laravel?

Cách gửi thông báo đẩy trên web trong Laravel 9 bằng Firebase .
Bước 1. Tạo dự án Laravel
Bước 2. kết nối cơ sở dữ liệu
Bước 3. Cài đặt giàn giao diện người dùng Laravel
Bước 4. Cập nhật Server Key Prop trong bảng người dùng
Bước 5. Nhận khóa máy chủ nhắn tin qua đám mây Firebase
Bước 6. Tạo bộ điều khiển
Bước 7. Tạo tuyến đường