【问题标题】:Dynamic desktop push notifications - PHP动态桌面推送通知 - PHP
【发布时间】:2017-01-14 00:14:05
【问题描述】:

对于发送桌面通知,我使用此代码。 (来自https://developer.mozilla.org/en/docs/Web/API/notification

<button onclick="notifyMe()">Notify me!</button>

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

  // Let's check whether notification permissions have already 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.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.
}

当我按下按钮时,这将在同一页面上工作,但是如果我想动态管理这些通知该怎么办? 即如果我从后端(管理员)推送通知,它应该向前端显示消息(允许通知的用户)

【问题讨论】:

  • 这有点棘手,你是用iis还是apache?
  • Apache,我认为 rest api 在这里会有所帮助。但我是新手,不确定

标签: javascript php jquery push-notification html5-notifications


【解决方案1】:

按照这些步骤进行

  1. 在用户端有一个函数 {notifyMe(msg)}。
    在 Javascript 中定义一个函数,如问题 notifyMe();
  2. 在特定时间间隔内发送 ajax 调用以检查当前登录用户的管理员状态
    在特定的时间间隔内向服务器发送 ajax 调用。在 setInterval 函数中调用 Ajax,每 20 秒发送一次请求。
  3. 如果管理员状态设置为消息或标志,则将消息传递给您定义的函数 {notifyMe(msg)}。用户会被自动注意到。
    在服务器端脚本中,检查管理员是否为用户(比如用户 ID 10)启用了通知。如果在服务器端设置为启用(用户 id 10),则在 ajax 返回调用您的函数 notifyMe()。一旦您决定停止通知,请清除 javascript 中的时间间隔。

您可以从会话或 Ajax 调用的参数中获取服务器端脚本中的用户 ID。因此,根据用户 ID,您将执行是否调用 Ajax 和 notifyMe()

【讨论】:

  • 你能详细说明你的答案吗?
  • @Rijo 更新了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多