简体   繁体   English

PHP Google推送通知

[英]PHP Google Push Notifications

I've been doing research on how to use ((google push notifications)) with PHP, but there aren't many working examples/documentation out there... 我一直在研究如何在PHP中使用((Google推送通知)),但是那里没有很多工作示例/文档...

I'm trying to get this to work on an internal network, that isn't opened to the outside, over HTTP. 我正在尝试使其在内部网络上工作,该内部网络未通过HTTP对外开放。

I was able to get this to work over HTTPS from our outward facing domain, but the internal attempt is telling me in the Chrome Console that "the API may no longer be used from insecure origins" , which I found means it requires HTTPS. 我能够从外部域通过HTTPS进行操作,但是内部尝试是在Chrome控制台中告诉我"the API may no longer be used from insecure origins" ,我发现这意味着它需要HTTPS。

Does anyone know of a possible workaround for this, to spoof the HTTPS, or to allow it to continue over HTTP? 有谁知道为此可能的解决方法,以欺骗HTTPS或允许其继续通过HTTP? I understand google put this in place as a preventative measure for people with malicious intent, but that's not the case here, as it'll be for private use. 我了解到google将其实施为有恶意意图的人的预防措施,但事实并非如此,因为它将用于私人用途。

My HTML is just a button with an onclick="notifyMe()" event. 我的HTML只是一个带有onclick="notifyMe()"事件的按钮。

Script to request permission, and function to notify: 请求权限的脚本,并用于通知以下功能:

<script>
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
  if (!Notification) {
    alert('Desktop notifications not available in your browser. Try Chromium.'); 
    return;
  }

  if (Notification.permission !== "granted")
    Notification.requestPermission();
});

function notifyMe() {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "Hey there! You've been notified!",
    });

    notification.onclick = function () {
      window.open("http://example.com");      
    };

  }

}
</script>

JS file with Notification Options, and permissions: 具有通知选项和权限的JS文件:

'use strict';

var button = document.querySelector('button');
var input = document.querySelector('input');

var notify = function() {
  var options = {
    body: input.value,
    icon: 'icon.png',
    tag: 'foo',
    type: 'basic'
  };
  var n = new Notification('Greetings!', options);
  n.onclick = function() {
    console.log('Clicked.');
  };
  n.onclose = function() {
    console.log('Closed.');
  };
  n.onshow = function() {
    console.log('Shown.');
  };
};

button.onclick = function() {
  if (!('Notification' in window)) {
    alert('This browser does not support desktop notification');
  } else if (Notification.permission === 'granted') {
    notify();
  } else if (Notification.permission !== 'denied') {
    Notification.requestPermission(function(permission) {
      if (!('permission' in Notification)) {
        Notification.permission = permission;
      }
      if (permission === 'granted') {
        notify();
      }
    });
  }
};

After doing extensive research and testing on this, I found that it isn't possible to use the Google Chrome notifications without an HTTPS connection, even if it's on an internal network. 在对此进行了大量研究和测试之后,我发现即使没有HTTPS连接,也无法使用Google Chrome通知,即使该通知位于内部网络中也是如此。

I've opted for setting up an SSL Cert, and that has resolved my problem. 我选择设置SSL证书,这已经解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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