2016-10-07 105 views
-1

我試圖創建一個功能,一旦按下按鈕,它會向網站上登錄的所有用戶發送一個chrome通知,除了按下按鈕的用戶以外,它們存儲在用戶數據庫中的表。現在,爲了簡單起見,我已經做了一個確認按鈕被單擊的警報,所以通知還沒有寫入。我擁有的代碼如下。發送通知給用戶

<!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 

</head> 
<body> 
    <div class="button"> 
     <form method="post"> 
      <input name="send" type="hidden" value="id"> <input name="send" 
      type="submit" value="knop"> 
     </form> 
    </div> 

//if the 'send' button is clicked, it echoes a script saying the button is clicked. 
if (isset($_POST['send'])) 
    { 
     echo "<script>alert(\"the button is clicked\")</script>"; 
// if the username is logged in AND the message is sent it should send the code script alert to all existing users in the 'user' table in the database. 

     if (isset($_SESSION['username']) AND ('$_POST[id]', '$_POST[name]', '$_POST[message]') 
    { 
echo "<script>alert(\"yes\")</script>"; 
} 
} 
    </body> 
    </html> 

最後一條if語句是錯誤的,我知道。這是因爲我不知道邏輯是什麼,所以我可以將條件設​​置爲用戶已登錄,並且該消息是由登錄用戶以外的人員發送的。 (所以發佈評論有效地發送通知給所有其他登錄在網站上的用戶)。

我希望我很清楚你能幫助我。

+1

那麼,如果它是活着的通知,你將需要使用websockets .. – Naruto

+0

謝謝,我不知道。你能詳細說明一下嗎? – Zhyohzhy

+0

您說過「所有用戶都註冊並存儲在用戶表中」和「所有其他用戶都登錄了該站點」 - 您是指哪一個? – Quentin

回答

0

這僅適用於local切勿使用database進行測試。

這是Notification的示例代碼。您只需在編輯器中複製並粘貼,並將其保存爲.php擴展名。該文件必須放置在www/。並運行它。變量var articles保存推送通知的數據。並且setTimeout(function(){這個函數被稱爲timing用於兩個不同的通知。

<?php 
$var; 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Display browser Notification from Web Application Demo</title> 
<script type="text/javascript"> 

var articles = [ 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"], 
["Send notification to users","http://stackoverflow.com/questions/39915185/send-notification-to-users"] 
]; 


setTimeout(function(){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
}, 200000); 



document.addEventListener('DOMContentLoaded', function() 
{ 

if (Notification.permission !== "granted") 
{ 
Notification.requestPermission(); 
} 

document.querySelector("#notificationButton").addEventListener("click", function(e) 
{ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 
}); 

//=================================== 
setInterval(function(e){ 
var x = Math.floor((Math.random() * 10) + 1); 
var title=articles[x][0]; 
var desc='Most popular article.'; 
var url=articles[x][1]; 
notifyBrowser(title,desc,url); 
e.preventDefault(); 

}, 5000); 
//=================================== 
}); 

function notifyBrowser(title,desc,url) 
{ 
if (!Notification) { 
console.log('Desktop notifications not available in your browser..'); 
return; 
} 
if (Notification.permission !== "granted") 
{ 
Notification.requestPermission(); 
} 
else { 
var notification = new Notification(title, { 
icon:'https://cdn0.iconfinder.com/data/icons/basic-ui-elements-colored/700/09_bell-3-512.png', 
body: desc, 
}); 

// Remove the notification from Notification Center when clicked. 
notification.onclick = function() { 
window.open(url);  
}; 

// Callback function when the notification is closed. 
notification.onclose = function() { 
console.log('Notification closed'); 
}; 

} 
} 


</script> 



<style type="text/css"> 
.hover{background-color: #cc0000} 
#container{ margin:0px auto; width: 800px} 
.button { 
font-weight: bold; 
    padding: 7px 9px; 
    background-color: #5fcf80; 
    color: #fff !important; 
    font-size: 12px; 
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; 
    cursor: pointer; 
    text-decoration: none; 
    text-shadow: 0 1px 0px rgba(0,0,0,0.15); 
    border-width: 1px 1px 3px !important; 
    border-style: solid; 
    border-color: #3ac162; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    display: -moz-inline-stack; 
    display: inline-block; 
    vertical-align: middle; 
    zoom: 1; 
    border-radius: 3px; 
    box-sizing: border-box; 
    box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset; 
} 
.authorBlock{border-top:1px solid #cc0000;} 
</style> 
</head> 
<body> 
<div id="container"> 
<h1>Display browser Notification from Web Application Demo</h1> 

<h4>Click notification button</h4> 
<a href="#" id="notificationButton" class="button">Notification</a> 
</div> 


</body> 
</html> 
+1

由於該問題基本上是關於服務器推送的,因此提供客戶端替代'alert'並不是真的有幫助。 – Quentin

+0

好吧謝謝,可能是他嘗試轉換成服務器推送。我只是想一個主意。 bcz他不知道邏輯@Quentin – Karthi

+1

OP確實知道邏輯。他們只是使用警報而不是通知API。問題在於爲所有用戶觸發,而不是通知用戶界面。 – Quentin