简体   繁体   English

Web通知无法在chrome上运行,但在Firefox和Microsoft Edge上可以正常运行

[英]Web notifications is not working on chrome, but it's working fine on firefox and microsoft edge

I am trying to make web notification, which is worked fine on firefox and microsoft edge. 我正在尝试发出Web通知,该通知在Firefox和Microsoft Edge上运行良好。 even it worked fine on safari. 即使在Safari上也能正常工作。 but it does not want to work on chrome. 但它不想在Chrome上运行。 and it shows no error. 它没有显示错误。 this is my code: 这是我的代码:

    <script>
    window.setInterval(function(){notifyMe();}, 5000);
    function notifyMe() {
      if (!("Notification" in window)) {
        alert("This browser does not support desktop notification");
      } else if (Notification.permission === "granted") {
          var data = []
          data['title'] = "notification's title";
          data['body'] = "notification's body";
          var notification = new Notification(data['title'], {
            'body': data['body']
          });
          notification.onclick = function(event) {
            window.open('https://www.example.com/', '_blank');
          }
      } else if (Notification.permission !== "denied") {
        Notification.requestPermission().then(function (permission) {
          if (permission === "granted") {
            var notification = new Notification("really");
          }
        });
      }
    }
</script>

Is your website served using https:// - because Chrome has deprecated Notifications API on insecure (eg http:// ) origins 您的网站是否使用https:// -因为Chrome在不可靠的来源(例如http:// )上弃用了Notifications API

Otherwise, your code works just fine in Chrome 否则,您的代码就可以在Chrome中正常运行

web Notification 网络通知

HTTPS (Chrome default mode) HTTPS(Chrome默认模式)

localhost debug 本地调试

HTTP HTTP

chrome://settings/content/notifications 铬://设置/内容/通知

https://pushassist.com/knowledgebase/how-to-enable-web-push-notifications-in-google-chrome-non-ssl-site/ https://pushassist.com/knowledgebase/how-to-enable-web-push-notifications-in-google-chrome-non-ssl-site/

  1. open chrome://flags/ , then search notification 打开chrome://flags/ ,然后搜索notification

  2. enable notification 启用通知

在此处输入图片说明

demo codes 演示代码

 "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * @created 2019-08-17 * * @description * @augments * @example * @link * */ let log = console.log; const webNotificationApp = (debug = false) => { try { if ("Notification" in window) { // let ask = window.Notification.requestPermission(); let ask = Notification.requestPermission(); ask.then( // Permission (permission) => { log(`permission =`, permission); if (permission === "granted") { log(`permission granted`); let msg = new Notification("App Upgrade Info", { body: "a new version app is available, click download: https://app.xgqfrms.xyz/download", icon: "https://cdn.xgqfrms.xyz/logo/icon.png", }); msg.addEventListener(`click`, (e) => { let btn = e.target.dataset(`btn-type`); if (btn === "ok") { log(`OK`); } else { log(`Cancel`); } alert(`clicked notification`); }); }else { log(`notification permission is denied!`); } } ) } else { console.warn(`your browser is too old, which not support web notification!`); } } catch (err) { console.error(`error =`, err); } }; document.addEventListener(`DOMContentLoaded`, () => { log(`DOMContentLoaded`); webNotificationApp(); }); // export default webNotificationApp; // export { // webNotificationApp, // }; 

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

相关问题 Javascript 在 Edge 中不起作用,但在 Firefox、Chrome 中很好 - Javascript not working in edge, but fine in Firefox, Chrome XPath选择在Microsoft Edge中不起作用(适用于Chrome和Firefox) - XPath selection not working in Microsoft Edge (works in Chrome and Firefox) FCM通知适用于Chrome,但不适用于Firefox - FCM notifications working on Chrome but not on Firefox jQuery无法在Chrome中运行,但Firefox可以 - JQuery not working in Chrome but Firefox is fine JavaScript适用于IE和Edge,但不适用于Chrome和Firefox - JavaScript working on IE and Edge but not working on Chrome and Firefox 为什么 body.scrollTop 不适用于 Google chrome 和 Firefox,但适用于 Microsoft Edge - Why body.scrollTop is not working on Google chrome and Firefox but working on Microsoft edge ASCII代码在firefox中不起作用,但在chrome上工作正常 - ASCII code not working in firefox but on chrome working fine 样式表在 chrome 中不起作用,在 Firefox 中工作正常 - stylesheet not working in chrome , working fine in firefox 下载画布作为图像无法在firefox和microsoft边缘工作 - Download canvas as image not working in firefox and microsoft edge 显示错误无法在 Microsoft Edge 中读取 null 的属性“拆分”,但在 chrome 上工作正常 - Showing Error Cannot read property 'split' of null in microsoft edge, but working fine on chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM