我正在尝试在基于PHP的网站上实现推送通知。其目标是制作类似于Stackoverflow和其他网站的东西,当他们收到消息时实时通知用户。
我使用mysql作为我的数据库,Apache作为我的服务器,并考虑使用Amazon-SNS作为这些通知的框架,因为这似乎是该服务的目的。
从文献中我很难理解sending.php和receiving.php页面是如何以编程方式设置的。我假设sending.php页面只包含某个页面的$_POST['message'],但从那里我真的迷失了方向。
如果有什么可以帮助我理解推送通知的receiving.php页面是什么样子,我将非常感谢。
发布于 2012-07-22 02:01:03
工作中
HTML5rocks已经很好地解释了here,关于websockets是如何工作的。
您可以在支持Websockets的浏览器中使用Websockets(因为所有现代浏览器都提供了很好的支持)
快速入门
您可以从以下几个资源开始:
HTML5rocks
Nettuts+
Nettuts+为websockets入门提供了一个很好的教程。
For Browsers Supporting Websockets
回退
您可以使用Modernizr来检测客户机的浏览器是否支持Websockets &作为备用,您可以使用flash而不是websockets。
对于这些项目,当在没有WebSockets或禁用它的浏览器上运行时,将使用web-socket-js。它的效率低于原生轮询,但延迟仍然比长轮询低得多。
任何带有Flash的浏览器都可以使用web-socket-js填充/多边形填充来支持WebSocket。
参考资料:
发布于 2013-02-19 00:16:08
我只想分享我使用的实际实现。我决定使用一个很棒的SAAS,Pusher,因为在实现推送通知时有许多具有挑战性的问题,正如我在阅读@Virendra的优秀答案中的链接时所意识到的那样,Pusher为你解决了这些问题。
最让我印象深刻的是,你只需编写很少的代码就能实现这一点。见下文。我的服务器端是PHP (Pusher has libraries in many languages)。
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>https://stackoverflow.com/questions/11592783
复制相似问题