PHP-小米推送-服务端

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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个设备
         }
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值