简体   繁体   English

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

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

I have gone through so many web pages but unable to find proper answer, can you help me with it. 我浏览了许多网页,但是找不到正确的答案,您能帮我吗? I want to know how one can create web push notification(not for android app, but for website only)?? 我想知道如何创建网络推送通知(不是用于Android应用程序,而是仅用于网站)?

You can simply add this line of code: 您可以简单地添加以下代码行:

var myNotification = new Notification(title, options);

I give you a better example, you have to ask to the user if you can add notifications: 我举一个更好的例子,您必须询问用户是否可以添加通知:

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

You may consult the MDN's documentation and the MDN's specification . 您可以查阅MDN的文档MDN的规范

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

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