[英]How do push notifications work?
我正在尝试在基于PHP的网站上实现推送通知。 目标是使类似Stackoverflow和其他站点的内容类似于在获取消息时实时通知用户。
我使用mysql作为我的数据库,Apache作为我的服务器,我正在考虑使用Amazon-SNS作为这些通知的框架,因为这就是该服务似乎的目的。
我从文献中了解到如何以编程方式设置sending.php
和receiving.php
页面。 我假设sending.php
页面只涉及$_POST['message']
到某个页面,但是从那里我真的迷路了。
如果有什么东西可以帮助我理解receiving.php
页面对推送通知的看法,我将非常感激。
HTML5rocks在这里提供了一个很好的解释,关于websockets如何工作。
那么你可以为支持它的浏览器使用Websockets(因为所有现代浏览器都提供了很好的支持)
您可以开始使用以下几种资源:
Nettuts +为websockets入门提供了一个很好的教程。
您可以使用Modernizr来检测客户端的浏览器是否支持websockets,作为后备,您可以使用flash而不是Websockets。
对于那些项目,当在没有WebSockets或禁用它的浏览器上运行时,将使用web-socket-js 。 它将比本机效率低,但仍然比长轮询更低的延迟。
任何带Flash的浏览器都可以使用web-socket-js shim / polyfill支持WebSocket。
参考:
我只想分享我的实际实现。 我决定选择一个优秀的SAAS, Pusher ,因为在实现Push通知时存在许多具有挑战性的问题,因为我在阅读@ Virendra的优秀答案中的链接时,Pusher为您解决了这个问题。
我印象最深刻的是你需要编写多少代码来完成这项工作。 见下文。 我的服务器端是PHP( Pusher有多种语言的库 )。
require('/application/thirdParty/pusher-html5-realtime-push-notifications/lib/squeeks-Pusher-PHP/lib/Pusher.php');
require('/application/thirdParty/pusher-html5-realtime-push-notifications/config.php');
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
foreach($recipients as $row){
$channel='my-channel'.$row->recipient_id;
$pusher->trigger($channel, 'notifications',
array('message' => $row->message,
'notification_id' => $row->notification_id)
);
}
这是HTML / JS(不要被淹没,这些代码的大部分只是填充小圆圈和带有传入通知的列表,如Stackoverflow和其他人这样做):
<script src="/application/thirdParty/pusher.min.js"></script>
<script>
var myID=179; // would receive notification if myID matches $row->recipient_id above;
var myChannel = pusher.subscribe('my-channel'+myID);
myChannel.bind('notifications',
function(data) {
var message=String(data.message),
url='/notifications/'+data.notification_id,
icon='<i class=\'icon-heart\'></i>',
urlText=icon+message;
var notificationRow='<li><a href='+url+'>'+urlText+'</a></li>';
$('#notificationsDropdownList').prepend(notificationRow);
if(notificationCircleCount==0){
notificationCircleCount++;
$notificationCircle.show();
$notificationCircleCount.html(notificationCircleCount);
}
else{
notificationCircleCount++;
$notificationCircleCount.html(notificationCircleCount);
}
console.log('Pusher happened'+data.message);
} //function
); //myChannel
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.