【问题标题】:Web notification from server来自服务器的 Web 通知
【发布时间】:2014-03-12 04:34:23
【问题描述】:

我想在更新数据库时从服务器向客户端推送通知。
这是我的控制器:

public function finish() {
    $this->autoRender = false;
    try {
        if($this->request->is('post')) {          
            $db = $this->ClientCabaInfo->getDataSource();
            if($this->Session->check('client_check_data')) {
                $data = $this->Session->read('client_check_data');
                ...
                if($this->CabaInfo->save()) {
                    $this->webNotify();
                }
            }

            $this->redirect('index');
        }

    } catch (Exception $e) {
        $this->EasyLog->log(Conf::r("APP.LOG.APP_FILE"),$e->getMessage(),Conf::r("COM.LOG.FORMAT_ERROR"),'NASUCT');
        $this->redirect("/errors/");
    }

}
public function webNotify() {
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');

    $time = date('r');
    echo "data: {$time}";
    flush();

}

在我看来,我打电话给:

if(typeof(EventSource)!=="undefined")
  {

  var source=new EventSource("<?php echo $this->Html->url('/client_caba/finish'); ?>");
  source.onmessage=function(event)
    {
        notifyMe(event.data);
    };
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support server-sent events...";
  }

但是当我访问页面时:http://localhost:8080/admin/client_caba/finish
它显示一个对话框并且没有完成操作......

我哪里错了?请告诉我如何解决这个问题并将参数传递给弹出通知......谢谢

【问题讨论】:

    标签: php html cakephp web-notifications


    【解决方案1】:

    您使用的是哪种浏览器? 显然,如果您看到已发布的对话框,则表示一切正常,并且您实际上正在接收来自服务器的响应。 对我来说,这似乎是一个浏览器问题。最新的浏览器版本(如 chrome)需要明确的权限才能显示桌面通知。 请在 chrome 中尝试您的代码,并注意一个小的弹出通知,要求您允许显示来自您的站点的通知。一旦你允许,事情应该可以正常工作。 如果有任何问题,请分享 notifyMe() 函数的代码,因为这可能会有一些不一致。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多