PHP-小米推送-服务端

SDK下载地址:https://dev.mi.com/mipush/downpage/

在WWW创建miPush,把SDK解压到WWW/miPush

我这边可能会有2种推送方法,使用 sendType 字段 实现,直接看代码:(我创建的接口名:gsw_android_mipush)

<?php
use xmpush\Builder;
use xmpush\Sender;
use xmpush\Constants;

include_once(dirname(__FILE__) . '/autoload.php');

// http://127.0.0.1/mipush/gsw_android_mipush.php?sendType=0&notifyId=0&userAccount=&title=标题111&desc=内容222&payload=          // 给全部用户发送
// http://127.0.0.1/mipush/gsw_android_mipush.php?sendType=1&notifyId=0&userAccount=123,333&title=标题333&desc=内容444&payload=   // 群发给123,333

$sendType = $_GET['sendType'];          //   0→向所有设备发送消息      1→指定userAccount群发
$notifyId = $_GET['notifyId'];          //   通知编号       相同编号的通知会互相覆盖
$userAccount = $_GET['userAccount'];    //   指定userAccount群发 的 userAccount数组   1000次/s     单条消息体最多携带1000个设备ID
$title = $_GET['title'];                //   推送标题
$desc = $_GET['desc'];                  //   推送内容
$payload = $_GET['payload'];            //   推送携带数据→键值对     暂时用不到

$push = new \push();                                                            // 实例化下面的类
$push->pushFun($sendType, $notifyId, $userAccount, $title, $desc, $payload);     // 调用函数

echo "<br><br>"."sendType = ".$sendType."<br>"."notifyId = ".$notifyId."<br>"."title = ".$title."<br>"."desc = ".$desc."<br>"."payload = ".$payload."<br><br>";  // 打印主要参数
var_dump($userAccount); // 打印 userAccount数组

class push
{
    public function pushFun($style,$notifyId,$userAccount,$title,$desc,$payload)
    {
        $secret = 'xxxxxxxxxxxx';      // XX 小米推送秘钥
        $package = 'com.xxxxx';        // XX 包名
        Constants::setPackage($package);        // 常量设置必须在new Sender()方法之前调用
        Constants::setSecret($secret);          // 常量设置必须在new Sender()方法之前调用
        $sender = new Sender();
        $message1 = new Builder();
        $message1->title($title);                           // 通知栏的title
        $message1->description($desc);                      // 通知栏的descption
        $message1->passThrough(0);            // 这是一条通知栏消息,如果需要透传,把这个参数设置成1,同时去掉title和descption两个参数
        $message1->payload($payload);                       // 携带的数据,点击后将会通过客户端的receiver中的onReceiveMessage方法传入。
        $message1->extra(Builder::notifyForeground, 1);     // 应用在前台是否展示通知,如果不希望应用在前台时候弹出通知,则设置这个参数为0
        $message1->notifyId((int)$notifyId);                // 通知类型。最多支持0-4 5个取值范围,同样的类型的通知会互相覆盖,不同类型可以在通知栏并存   左边的官方文档,实际测试,随便取值0-9999999都可以。
        $message1->build();
        $id = (int)$style;
        if ($id == 0) {
            print_r($sender->broadcastAll($message1)->getRaw());                        // 向所有设备发送消息。
        }else if ($id == 1){
            print_r($sender->sendToUserAccount($message1, $userAccount)->getRaw());     // 指定userAccount群发。userAccount:Android端自定义,10个设备可是使用相同的userAccount,第11个设备会顶掉第1个设备
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/iwanghang/p/9056796.html