php做推送服务端实现android消息推送

本文介绍了如何使用PHP搭建推送服务端,通过解析下载并配置tokudu-PhpMQTTClient和rsmb软件包,将相关文件放置在指定目录,以实现对Android设备的消息推送功能。主要涉及send_mqtt.php主文件及SAM目录下的MQTT相关PHP文件。
摘要由CSDN通过智能技术生成

下载tokudu-PhpMQTTClient-ba4e494.zip

下载rsmb_1.2.0.zip(windows环境下要开\windows\broker.exe   切记切记

 

解压tokudu-PhpMQTTClient-ba4e494.zip

www下边新建目录androidpush

把解压文件tokudu-PhpMQTTClient-ba4e494中的SAM文件夹copy到androidpush目录下

把解压文件tokudu-PhpMQTTClient-ba4e494中的send_mqtt.php文件copy到androidpush目录下

结构如图:

 

send_mqtt.php主文件

<?php

require('SAM/php_sam.php');

//create a new connection object
//创建一个新的连接对象
$conn = new SAMConnection();

//start initialise the connection
//开始初始化连接 SAM_HOST服务器host路径 ;SAM_PORT服务器port端口号
$conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1',
                               SAM_PORT => 1883));      
//create a new MQTT message with the output of the shell command as the body
//建立一个新的MQTT shell命令的输出消息以作为主体($msgCpu是通知内容) 
//new SAMMessage() 参数一般写为json格式
$msgCpu = new SAMMessage("测试通知");

//send the message on the topic cpu
//发送该信息的主体
//860173018344139是设备号,每一台android手机对应一个唯一的设备号 
//$msgCpu通知内容
//send()推送通知
$conn->send('topic://'.'860173018344139', $msgCpu);

//关闭连接
$conn->disconnect();         

echo 'MQTT Message to ' . '860173018344139' . ' sent: ' . '测试通知'; 

?>


SAM目录

SAM/MQTT/sam_mqtt.php

<?php
/*
+----------------------------------------------------------------------+
| Copyright IBM Corporation 2006, 2007.                                      |
| All Rights Reserved.                                                 |
+----------------------------------------------------------------------+
|                                                                      |
| Licensed under the Apache License, Version 2.0 (the "License"); you  |
| may not use this file except in compliance with the License. You may |
| obtain a copy of the License at                                      |
| http://www.apache.org/licenses/LICENSE-2.0                           |
|                                                                      |
| Unless required by applicable law or agreed to in writing, software  |
| distributed under the License is distributed on an "AS IS" BASIS,    |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
| implied. See the License for the specific language governing         |
| permissions and limitations under the License.                       |
+----------------------------------------------------------------------+
| Author: Dave Renshaw                                                 |
+----------------------------------------------------------------------+

$Id: sam_mqtt.php,v 1.1 2007/02/02 15:36:46 dsr Exp $

*/

define("SAM_MQTT_CLEANSTART", "SAM_MQTT_CLEANSTART");
define("SAM_MQTT_QOS", "SAM_MQTT_QOS");
define("SAM_MQTT_SUB_SEPARATOR", "#-#");
/* ---------------------------------
    SAMConnection
   --------------------------------- */

class SAMConnection_MQTT {

  var $debug = false;

  var $errno = 0;
  var $error = '';

  /*
   Info we need to keep between calls...
  */
  var $sub_id = '';
  var $port = '';
  var $host = '';
  var $cleanstart = false;
  var $virtualConnected = false;
  var $connected = false;
  /*
   Our current open socket...
  */
  var $sock;

  /*
   Table of available operations using the MQTT protocol...
  */
  var $operations = array("MQTT_CONNECT"     => 1,
                          "MQTT_CONNACK"     => 2,
                          "MQTT_PUBLISH"     => 3,
                          "MQTT_PUBACK"      => 4,
                          "MQTT_PUBREC"      => 5,
                          "MQTT_PUBREL"      => 6,
                          "MQTT_PUBCOMP"     => 7,
                          "MQTT_SUBSCRIBE"   => 8,
                          "MQTT_SUBACK"      => 9,
                          "MQTT_UNSUBSCRIBE" => 10,
                          "MQTT_UNSUBACK"    => 11,
                          "MQTT_PINGREC"     => 12,
                          "MQTT_PINGRESP"    => 13,
                          "MQTT_DISCONNECT"  => 14);

  /* ---------------------------------
      Constructor
     --------------------------------- */
  function SAMConnection_MQTT() {
    if ($this->debug) e('SAMConnection_MQTT()');

    if ($this->debug) x('SAMConnection_MQTT()');
  }

  /* ---------------------------------
      Commit
     --------------------------------- */
  function Commit() {
    if ($this->debug) e('SAMConnection_MQTT.Commit()');

    $errno = 100;
    $error = 'Unsupported operation for MQTT protocol!';
    $rc = false;

    if ($this->debug) x("SAMConnection_MQTT.Commit() rc=$rc");
    return $rc;
  }

  /* ---------------------------------
      Connect
     --------------------------------- */
  function Connect($proto, $options=array()) {
    if ($this->debug) e('SAMConnection_MQTT.Connect()');

    /* Check our optional parameter array for the necessary bits...   */
    if ($options[SAM_PORT] == '') {
        $this->port = 1883;
    } else {
        $this->port = $options[SAM_PORT];
    }
    if ($options[SAM_HOST] == '') {
        $this->host = 'localhost';
    } else {
        $this->host = $options[SAM_HOST];
    }

    $this->cleanstart = in_array(SAM_MQTT_CLEANSTART, $options);

    if ($this->debug) t("SAMConnection_MQTT.Connect() host=$this->host, port=$this->port, cleanstart=$this->cleanstart");

    if ($this->checkHost($this->host, $this->port)) {
        $this->virtualConnected = true;
    } else {
        $this->virtualConnected = false;
    }

    if ($this->debug) x("SAMConnection_MQTT.Connect() rc=$this->virtualConnected");
    return $this->virtualConnected;
  }

  /* ---------------------------------
      Disconnect
     --------------------------------- */
  function Disconnect() {
    if ($this->debug) e('SAMConnection_MQTT.Disconnect()');
    $rc = false;

    if ($this->virtualConnected) {
        if ($this->connected) {
            $msg = $this->fixed_header("MQTT_DISCONNECT").pack('C', 0);
            fwrite($this->sock, $msg);
            $response = fgets($this->sock, 128);
            if ($this->debug) t('SAMConnection_MQTT.Disconnect() response is '.strlen($response).' bytes');
            if (strlen($response) == 0) {
                fclose($this->sock);
                $this->sock = NULL;
            }
        }
        $this->virtualConnected = false;
        $this->connected = false;
        $rc = true;
    }

    if ($this->debug) x("SAMConnection_MQTT.Disconnect() rc=$rc");
    return $rc;
  }

  /* ---------------------------------
      IsConnected
     --------------------------------- */
  function IsConnected() {
    if ($this->debug) e('SAMConnection_MQTT.IsConnected()');
    $rc = false;

    if ($this->connected) {
        $rc = true;
    }

    if ($this->debug) x("SAMConnection_MQTT.IsConnected() rc=$rc");
    return $rc;
  }

  /* ---------------------------------
      Peek
     --------------------------------- */
  function Peek() {
    if ($this->debug) e('SAMConnection_MQTT.Peek()');

    $errno = 100;
    $error = 'Unsupported operation for MQTT protocol!';
    $rc = false;

    if ($this->debug) x("SAMConnection_MQTT.Peek() rc=$rc");
    return $rc;
  }

  /* ---------------------------------
      PeekAll
     --------------------------------- */
  function PeekAll() {
    if ($this->debug) e('SAMConnection_MQTT.PeekAll()');

    $errno = 100;
    $error = 'Unsupported operation for MQTT protocol!';
    $rc = false;

    if ($this->debug) x("SAMConnection_MQTT.PeekAll() rc=$rc");
    return $rc;
  }

  /* ---------------------------------
      Receive
     --------------------------------- */
  function Receive($sub_id, $options=array()) {
    if ($this->debug) e('SAMConnection_MQTT.Receive()');
    $rc = false;

    /* strip the topic from the rear of the subscription id...  */
    $x = strpos($sub_id, SAM_MQTT_SUB_SEPARATOR);
    if (!$x) {
        $this->errno = 279;
        $this->error = 'Specified subscription id ('.$sub_id.') is not valid!';
        return false;
    }
    $topic = substr($sub_id, $x + strlen(SAM_MQTT_SUB_SEPARATOR));
    $si = substr($sub_id, 0, $x);

    /* Are we already connected?               */
    if (!$this->connected) {
        if ($this->debug) t('SAMConnection_MQTT.Receive() Not connected.');
        /* No, so open up the connection...    */
        $this->sub_id = $si;
        $rc = $this->do_connect_now();
    } else {
        /* We are already connected. Are we using the right subscriber id?  */
        if ($this->sub_id != $si) {
            if ($this->debug) t('SAMConnection_MQTT.Receive() Connected with wrong sub_id.');
            /* No, We better reconnect then...  */
            $this->disconnect();
            $this->sub_id = $si;
            $rc = $this->do_connect_now();
        } else {
            if ($this->debug) t('SAMConnection_MQTT.Receive() Connected OK.');
            $rc = true;
        }
    }

    if ($rc) {

        /* have we got a timeout specified?    */
        if ($options[SAM_WAIT] > 1) {
            $m = $options[SAM_WAIT] % 1000;
            $s = ($options[SAM_WAIT] - $m) /1000;
            if ($this->debug) t('SAMConnection_MQTT.Receive() timeout='.$options[SAM_WAIT]." ($s secs $m millisecs)");
            stream_set_timeout($this->sock, $s, $m);
            if (
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值