简体   繁体   English

在PHP中推送通知

[英]Push notifications in php

How to implement push notifications in PHP based webapp. 如何在基于PHP的webapp中实现推送通知。 What I actually want to implement is to get a notification as soon as some user joins a chat. 我真正想要实现的是在某些用户加入聊天后立即收到通知。

suppose two users are participants in a chat, if a third user joins that chat, then other two user be notified. 假设两个用户是聊天的参与者,如果第三个用户加入了该聊天,则将通知另外两个用户。

I think you mean notifications to the user using Javascript. 我认为您是指使用Javascript向用户发送通知。 This could be done using websockets (HTML5) or javascript that every x seconds makes a call to your server. 可以使用websockets(HTML5)或javascript来完成,每隔x秒就会调用一次服务器。

Websockets are hard to setup are my experience. Websockets很难设置是我的经验。

every x seconds is, 1 a waste of resource often and leads to a lot of trouble when you get a lot of visitors. 每x秒通常会浪费资源1,当您吸引大量访问者时会带来很多麻烦。

The solution I use is an api called pusher . 我使用的解决方案是一个称为pusher的api。 I use it only for apps for my self so I use the free version. 我仅将其用于自己的应用程序,因此我使用免费版本。 But it works like a charm and is easy to setup 但是它就像一个魅力,很容易设置

Also look at http://en.wikipedia.org/wiki/Push_technology , from Anigel 另请参见Anigel的http://en.wikipedia.org/wiki/Push_technology

What about adding NodeJS + SocketIO to your stack? 如何将NodeJS + SocketIO添加到堆栈中?

SocketIO let's you create a room for your clients and it can emit messages to a room of clients. SocketIO让您为客户创建一个房间,它可以向客户房间发送消息。

Let me show you a basic example: 让我向您展示一个基本示例:

notification.js notification.js

var app = require('http').createServer(), 
    io = require('socket.io').listen(app);

app.listen(3000);

io.sockets.on('connection', function (socket) {
    // Joining a room
    socket.join('chatroom');

    socket.on('newUserJoinRequest', function(data) {
        socket.broadcast.to('chatroom').emit('newUserJoinedNotification', data);
    })

});

client.html client.html

<!DOCTYPE html>
<head>   
    <meta charset="utf-8">    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
    <script type="text/javascript" src="http://localhost:3000/socket.io/socket.io.js"></script>
</head>
<body>
    <input type="text"  id="username" placeholder="name">
    <button type="text" id="joinButton">Join</button>
    <div id="responses">   
        <h2>NodeJS+SocketIO responses:</h2>   
        <ul></ul>
    </div>

    <script>
        var socket;
        jQuery(document).ready(function() {
            socket = io.connect('//localhost:3000');

            socket.on('newUserJoinedNotification', function(data) {
                var li = '<li>A new user joined the room: ' + data.name + '</li>';    
                jQuery('#responses ul').append(li);
            });
        });

        jQuery('#joinButton').click(function() {
            var username = jQuery('#username').val();
            socket.emit('newUserJoinRequest', {name: username});
        });
    </script> 

</body>
</html>

True push from server to client is not (easily) possible with PHP. 使用PHP无法(轻松)将服务器真正推向客户端。 What you will need is polling. 您将需要进行轮询。 You can create a message queue on the server for each connected client and the client automatically retrieves the contents of that queue with a poller (so basically doing requests every set interval). 您可以在服务器上为每个已连接的客户端创建一个消息队列,并且客户端会使用轮询器自动检索该队列的内容(因此基本上是在每个设置的时间间隔内执行请求)。

If you want it to be simple, then you need to look into either polling, long polling or using an api. 如果您希望它简单,那么您需要研究轮询,长时间轮询或使用api。 http://en.wikipedia.org/wiki/Push_technology http://en.wikipedia.org/wiki/Push_technology

There is no way using php to make the server open up a connection via proxies, firewalls etc to a program on a computer when that computer hasn't already got a connection to the server and said it is waiting for data. 当该计算机尚未与服务器建立连接并表示正在等待数据时,无法使用php使服务器通过代理,防火墙等打开与该计算机上程序的连接。

That is effectively what long polling does, the client makes a connection to the server and keeps it open waiting for the server to tell it something, or until it times out. 这实际上是长时间轮询所做的事情,客户端与服务器建立连接并使其保持打开状态,以等待服务器告知其某些信息或直到超时。 If it times out, then it reconnects 如果超时,则重新连接

You can set up push notifications without developing your own app using notify.pm. 您可以使用notify.pm设置推送通知,而无需开发自己的应用程序。 It takes around 2 lines of code, the API is here: https://github.com/notifypm/notifypm-php-api 它大约需要两行代码,API在这里: https : //github.com/notifypm/notifypm-php-api

<?php

require_once("notifypm.class.php");

$n = new Notifypm('email', 'password', '.pmNumber');
$n->push('Message!', 'Link (optional)!');

$n->disconnect();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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