简体   繁体   中英

Web notification from server

I want to push notification to client from server when I updated database.
This is my controller:

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();

}

And in my view, I called:

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...";
  }

But when I access page: http://localhost:8080/admin/client_caba/finish
It show a dialog and does not finish the action... 在此处输入图片说明

Where am I wrong? And please show me how to solved this problem and pass parameter to popup notification... Thanks

Which browser are you using? Apparently if you are seeing that dialog which you have posted, that means everything is working fine and you are actually receiving a response from the server. To me it seems like a browser issue. Latest browser versions like chrome need explicit permissions to display desktop notifications. Please try your code in chrome and watch for a small pop-up notification asking for permission to display notifications from your site. Once you allow that, things should work fine. If there is any problem then please share code of notifyMe() function as that might have some inconsistency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM