【问题标题】:How to send web push notification如何发送网络推送通知
【发布时间】:2019-05-24 13:58:54
【问题描述】:

我有一个 Angular PWA 在后端作为管理面板运行 Laravel。当管理员在后端创建新闻时,我想将网络推送通知发送到 PWA 应用程序。没有用户注册,它是一个简单的网站。

我更喜欢使用 On 信号或 FCM 推送通知。

任何有示例的有用网站将不胜感激。

【问题讨论】:

    标签: angular laravel push-notification


    【解决方案1】:

    您可以使用laravel + redis + socket.io + socket client 来做到这一点:

    $ cd your-project
    

    1.安装包:

    $ npm install express ioredis socket.io --save
    

    2。您的 package.json 文件将如下所示:

    {
      "private": true,
      "devDependencies": {
        "gulp": "^3.8.8",
        "laravel-elixir": "*"
      },
      "dependencies": {
        "express": "^4.12.4",
        "ioredis": "^1.4.0",
        "redis": "^0.12.1",
        "socket.io": "^1.3.5"
      }
    }
    
    composer require predis/predis
    

    3.在 Laravel 中创建事件:

    php artisan make:event WebPush
    

    4.整个 WebPush.php 类应如下所示:

    <?php namespace App\Events;
    
    use App\Events\Event;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Queue\SerializesModels;
    
    class WebPush extends Event implements ShouldBroadcast
    {
        use SerializesModels;
    
        public $data;
    
        public function __construct()
        {
            $this->data = array(
                'power'=> '10'
            );
        }
    
        public function broadcastOn()
        {
            return ['test-channel'];
        }
    }
    

    5.在 Angular 中使用此代码:

    <script src="js/socket.io.js'"></script>
        <script>
            //var socket = io('http://localhost:3000');
            var socket = io('http://192.168.10.10:3000');
            socket.on("test-channel:App\\Events\\EventName", function(message){
                // increase the power everytime we load test route
                $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
            });
        </script>
    

    6.像这样设置三个路线。将它们添加到您的 app/Http/routes.php 文件中:

    Route::get('/', function() {
        // this doesn't do anything other than to
        // tell you to go to /fire
        return "go to /fire";
    });
    
    Route::get('fire', function () {
        // this fires the event
        event(new App\Events\EventName());
        return "event fired";
    });
    
    Route::get('test', function () {
        // this checks for the event
        return view('test');
    });
    

    7.在项目根目录中创建socket.js 文件:

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var Redis = require('ioredis');
    var redis = new Redis();
    redis.subscribe('test-channel', function(err, count) {
    });
    redis.on('message', function(channel, message) {
        console.log('Message Recieved: ' + message);
        message = JSON.parse(message);
        io.emit(channel + ':' + message.event, message.data);
    });
    http.listen(3000, function(){
        console.log('Listening on Port 3000');
    });
    

    附注:

    确保您已安装 redis 服务器 + 使用 .env 或数据库文件将其连接到 Laravel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-14
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多