【问题标题】:PHP Google Push NotificationsPHP 谷歌推送通知
【发布时间】:2018-07-19 15:39:52
【问题描述】:

我一直在研究如何在 PHP 中使用((谷歌推送通知)),但是那里没有很多工作示例/文档...

我正在尝试通过 HTTP 使其在未向外部开放的内部网络上运行。

我能够从我们面向外部的域通过 HTTPS 运行它,但内部尝试在 Chrome 控制台中告诉我 "the API may no longer be used from insecure origins",我发现这意味着它需要 HTTPS。

有没有人知道可能的解决方法,欺骗 HTTPS,或允许它通过 HTTP 继续?我知道谷歌将这一点作为预防措施,以防范有恶意的人,但这里不是这种情况,因为它是供私人使用的。

我的 HTML 只是一个带有 onclick="notifyMe()" 事件的按钮。

请求权限的脚本和通知函数:

<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 文件:

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

【问题讨论】:

    标签: javascript php google-chrome push-notification


    【解决方案1】:

    在对此进行广泛的研究和测试后,我发现即使在内部网络上,也无法在没有 HTTPS 连接的情况下使用 Google Chrome 通知。

    我选择设置 SSL 证书,这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 2021-06-27
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 2015-09-28
      • 2013-08-20
      相关资源
      最近更新 更多