繁体   English   中英

创建网络推送通知(不是用于Android应用程序,而是仅用于网站)?

[英]create web push notification(not for android app, but for website only)?

我浏览了许多网页,但是找不到正确的答案,您能帮我吗? 我想知道如何创建网络推送通知(不是用于Android应用程序,而是仅用于网站)?

您可以简单地添加以下代码行:

var myNotification = new Notification(title, options);

我举一个更好的例子,您必须询问用户是否可以添加通知:

function notifyMe() {
  // Let's check if the browser supports notifications
  if (!("Notification" in window)) {
    console.log("This browser does not support desktop notification");
  }

  // Let's check whether notification permissions have alredy been granted
  else if (Notification.permission === "granted") {
    // If it's okay let's create a notification
    var notification = new Notification("Hi there!");
  }

  // Otherwise, we need to ask the user for permission
  else if (Notification.permission !== 'denied' || Notification.permission === "default") {
    Notification.requestPermission(function (permission) {
      // If the user accepts, let's create a notification
      if (permission === "granted") {
        var notification = new Notification("Hi there!");
      }
    });
  }

  // At last, if the user has denied notifications, and you 
  // want to be respectful there is no need to bother them any more.
}

您可以查阅MDN的文档MDN的规范

暂无
暂无

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

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