简体   繁体   English

推送通知如何工作?

[英]How do push notifications work?

I'm trying to implement push notifications on my PHP-based website. 我正在尝试在基于PHP的网站上实现推送通知。 The goal is to make something similiar to what Stackoverflow and other sites have which notify a user in real-time when they get messages. 目标是使类似Stackoverflow和其他站点的内容类似于在获取消息时实时通知用户。

I'm using mysql as my database, Apache as my server, and am considering using Amazon-SNS as the framework for these notifications since that is what that service seems to be intended for. 我使用mysql作为我的数据库,Apache作为我的服务器,我正在考虑使用Amazon-SNS作为这些通知的框架,因为这就是该服务似乎的目的。

I'm having a hard understanding from the literature how programmatically the sending.php and receiving.php pages are set up. 我从文献中了解到如何以编程方式设置sending.phpreceiving.php页面。 I'd assume the sending.php page just involves a $_POST['message'] to some page, but from there I'm really lost. 我假设sending.php页面只涉及$_POST['message']到某个页面,但是从那里我真的迷路了。

If something could help me understand what the receiving.php page would look like for a push notification I would greatly appreciate it. 如果有什么东西可以帮助我理解receiving.php页面对推送通知的看法,我将非常感激。

Working 工作

HTML5rocks have provided a good explanation here , about how websockets work. HTML5rocks在这里提供了一个很好的解释,关于websockets如何工作。

Well you can make use of Websockets for the browsers that support it (since all modern browsers provide good support) 那么你可以为支持它的浏览器使用Websockets(因为所有现代浏览器都提供了很好的支持)

Getting Started 入门

You can get started with these few resources : 您可以开始使用以下几种资源:

HTML5rocks HTML5ROCKS

Nettuts+ NETTUTS +

Nettuts+ provide a good tutorial for getting started with websockets . Nettuts +为websockets入门提供了一个很好的教程。

For Browsers Supporting Websockets 对于支持Websockets的浏览器

Fallback 倒退

You can use Modernizr to detect whether the Client's browser has support for websockets or not & as a fallback you can make use of flash instead of Websockets. 您可以使用Modernizr来检测客户端的浏览器是否支持websockets,作为后备,您可以使用flash而不是Websockets。

For those projects, when running on browsers with no WebSockets or with it disabled, web-socket-js will be used. 对于那些项目,当在没有WebSockets或禁用它的浏览器上运行时,将使用web-socket-js It will be less efficient than native, but still much lower latency than long-polling. 它将比本机效率低,但仍然比长轮询更低的延迟。

Any browser with Flash can support WebSocket using the web-socket-js shim/polyfill. 任何带Flash的浏览器都可以使用web-socket-js shim / polyfill支持WebSocket。

Reference: 参考:

Alternative to WebSockets WebSockets的替代品

https://softwareengineering.stackexchange.com/questions/33713/is-there-an-alternative-to-html-web-sockets-now-that-firefox-4-has-disabled-the https://softwareengineering.stackexchange.com/questions/33713/is-there-an-alternative-to-html-web-sockets-now-that-firefox-4-has-disabled-the

I just wanted to share the actual implementation I went with. 我只想分享我的实际实现。 I decided to go with a great SAAS, Pusher , since there are many challenging issues in implementing Push notifications, as I realized in reading the links in @Virendra's excellent answer, that Pusher solves for you. 我决定选择一个优秀的SAAS, Pusher ,因为在实现Push通知时存在许多具有挑战性的问题,因为我在阅读@ Virendra的优秀答案中的链接时,Pusher为您解决了这个问题。

What I was most impressed with is how little code you have to write to make this work. 我印象最深刻的是你需要编写多少代码来完成这项工作。 See below. 见下文。 My server-side is in PHP ( Pusher has libraries in many languages ). 我的服务器端是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) 
  );
}

Here's the HTML/JS (don't be overwhelmed, most of this code is just to populate the little circle and the list with the incoming notification as Stackoverflow and others do it): 这是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.

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