简体   繁体   English

动态桌面推送通知 - PHP

[英]Dynamic desktop push notifications - PHP

For send desktop notifications I use this code. 对于发送桌面通知,我使用此代码。 (from https://developer.mozilla.org/en/docs/Web/API/notification ) (来自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.
}

This will work on same page when I press button, but what should I do if I want to manage these notifications dynamically ? 当我按下按钮时,这将在同一页面上工作,但如果我想动态管理这些通知,我该怎么办? ie If I push notification from backend (admin) it should show message to front-end (users' who allow notifications) 即如果我从后端(管理员)推送通知,它应该向前端显示消息(允许通知的用户)

Follow these steps 跟着这些步骤

  1. Have a function {notifyMe(msg)} in user end. 在用户端有一个函数{notifyMe(msg)}。
    In Javascript define a function as like in question notifyMe(); 在Javascript中定义一个函数就像问题notifyMe();
  2. send an ajax call in a particular interval to check admin status for the current logged user 以特定间隔发送ajax呼叫以检查当前登录用户的管理状态
    Send an ajax call to the server in a particular interval. 以特定间隔向服务器发送ajax调用。 Call Ajax inside setInterval function and send request for every 20Sec. 在setInterval函数内调用Ajax并发送每20Sec的请求。
  3. If admin status is set with message or a flag, Pass the message to your defined function {notifyMe(msg)}. 如果使用消息或标志设置管理员状态,请将消息传递给定义的函数{notifyMe(msg)}。 User will be noticed automatically. 用户将被自动注意到。
    In server side script check if the notification is enabled for the user (say for user id 10) by administrator. 在服务器端脚本中,检查管理员是否为用户启用了通知(例如用户标识10)。 If it is set to enabled (user id 10) in serverside, then in ajax return call your function notifyMe() . 如果在服务器端设置为启用(用户ID 10),则在ajax返回中调用函数notifyMe() Once you determine to stop the notification clear the interval in javascript. 一旦确定停止通知,请在javascript中清除间隔。

You may get the user id in server side script either from session or a parameter from Ajax call. 您可以从会话或Ajax调用中的参数获取服务器端脚本中的用户标识。 So based on the user id you will perform whether to call or not to call Ajax and notifyMe() 因此,根据用户ID,您将执行是否调用Ajax和notifyMe()

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

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