How do push notifications work?

后端 未结 2 623
走了就别回头了
走了就别回头了 2020-12-23 12:07

I\'m trying to implement push notifications on my PHP-based website. The goal is to make something similiar to what Stackoverflow and other sites have which notify a user in

2条回答
  •  旧巷少年郎
    2020-12-23 12:37

    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.

    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).

    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):

    
    
    

提交回复
热议问题