码迷,mamicode.com
首页 > 其他好文 > 详细

TP5中使用极光推送3.5.12

时间:2017-06-21 11:25:04      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:php   tp5使用极光推送   thinkphp5-jpush3.5.12   

此前的文章中记录过在ThinkPHP3.2.3中使用极光推送的。

项目新使用了TP5的框架,遂就如此照搬迁移过来了。


先看TP5的目录架构说明:

project  应用部署目录
├─application           应用目录(可设置)
│  ├─common             公共模块目录(可更改)
│  ├─index              模块目录(可更改)
│  │  ├─config.php      模块配置文件
│  │  ├─common.php      模块函数文件
│  │  ├─controller      控制器目录
│  │  ├─model           模型目录
│  │  ├─view            视图目录
│  │  └─ ...            更多类库目录
│  ├─command.php        命令行工具配置文件
│  ├─common.php         应用公共(函数)文件
│  ├─config.php         应用(公共)配置文件
│  ├─database.php       数据库配置文件
│  ├─tags.php           应用行为扩展定义文件
│  └─route.php          路由配置文件
├─extend(主要)         扩展类库目录(可定义)#扩展存放位置#
├─public                WEB 部署目录(对外访问目录)
│  ├─static             静态资源存放目录(css,js,image)
│  ├─index.php          应用入口文件
│  ├─router.php         快速测试文件
│  └─.htaccess          用于 apache 的重写
├─runtime               应用的运行时目录(可写,可设置)
├─vendor                第三方类库目录(Composer)
......
省略
......

关于TP5的命名空间

我们只需要把自己的类库包目录放入EXTEND_PATH目录(默认为extend,可配置),就可以自动注册对应的命名空间,例如:
我们在extend目录下面新增一个my目录,然后定义一个\my\Test类( 类文件位于extend/my/Test.php)如下:
namespace my;class Test {    public function sayHello()
    {        echo ‘hello‘;
    }
}
我们就可以直接实例化和调用:
$Test = new \my\Test();
$Test->sayHello();
Jpush官方推荐使用composer来使用JPush,因对composer操作不娴熟,还是直接Download代码放到了extend目录使用了(这里之所以提到命名空间是因为极光的SDK也使用命名空间的概念):

技术分享

引入完之后不需要做改动的,即可使用。

放上推送类:

<?php
namespace app\api\controller;

use app\common\controller\Base as commonBase;
use think\Request;

class Push extends commonBase
{
    protected $staffModel;
    protected $attendModel;
    
    // 极光的key和secret,在极光注册完应用后,会生成
    protected $app_key = ‘bb9cc499f84c145****8a‘; //填入你的app_key
    protected $master_secret = ‘1f7d26a7e52****6b4434‘; //填入你的master_secret

    public function __construct(Request $request = null) 
    {
        parent::__construct($request);
    }
    
    /**
     * 推送所有人
     */
    public function push_test()
    {
        $alert = "您收到一条消息";
        $msg = array(
            "extras" => array(
                "name" => "sb-hongpeifeng",
                "id"    => 1,
                "idcard_no" => "41142419874562545",
                "id_img" => "http://000.000.000.000:9090/uploads/20170619/2d9e87fe911f5aa1af80f7c29d13c332.jpg",
                "rec_img" => "http://000.000.000.000:9090/uploads/20170619/3b9471fa251c837a437acc8259aeaf59.jpg",
                "gate_addr" => "1"
            )
        );

        $regid = ‘null‘;
        $result = notifyAllUser($alert, $msg, $this->app_key, $this->master_secret);
        var_dump($result);die;
    }
    
    /**
     * 推送给特定用户
     */
    public function push_special()
    {
        $alert = "您收到一条消息";
        $msg = array(
            "extras" => array(
                "name" => "sb-hongpeifeng",
                "id"    => 1,
                "idcard_no" => "41142419874562545",
                "id_img" => "http://000.000.000.000:9090/uploads/20170619/2d9e87fe911f5aa1af80f7c29d13c332.jpg",
                "rec_img" => "http://000.000.000.000:9090/uploads/20170619/3b9471fa251c837a437acc8259aeaf59.jpg",
                "gate_addr" => "1"
            )
        );

        $regid = ‘null‘;
        $result = sendNotifySpecial($regid, $alert, $msg, $this->app_key, $this->master_secret, $device_type = "all", $msg);
        var_dump($result);die;
    }


}

common.php

// 应用公共文件

/**
 * 向所有设备推送消息-广播
 * @param array $regid 特定设备的设备标识
 * @param string $message 需要推送的消息
 * return array
 */
function notifyAllUser($alert, $message, $app_key, $master_secret)
{
    $client = new \JPush\Client($app_key, $master_secret);

    $result = $client->push()
    ->addAllAudience() // 推送所有观众
    ->setPlatform(‘all‘)
    ->iosNotification($alert, $message)
    ->androidNotification($alert, $message)
    ->send();
    
    return json_array($result);
}

/**
 * 将数据先转换成json,然后转成array
 */
function json_array($result)
{
    $result_json = json_encode($result);
    return json_decode($result_json, true);
}

/**
 * 向特定设备推送消息
 * @param array $regid 接收推送的设备标识
 * @param string $message 需要推送的消息体
 * return array
 */
function sendNotifySpecial($regid, $alert, $message, $app_key, $master_secret)
{
    $client = new \JPush\Client($app_key, $master_secret);

    $result = $client->push()
    ->addAllAudience() // 推送所有观众
    ->setPlatform(‘all‘)
    // ->message($message, $msg) // 应用内消息
    ->addAlias($regid) // 给别名推送
    ->iosNotification($alert, $message)
    ->androidNotification($alert, $message)
    ->send();
    return json_array($result);
}


推送结果:

array(3) {
  ["body"]=>
  array(2) {
    ["sendno"]=>
    string(10) "1673772565"
    ["msg_id"]=>
    string(17) "24769798198999500"
  }
  ["http_code"]=>
  int(200)
  ["headers"]=>
  array(10) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"
    ["Server"]=>
    string(5) "nginx"
    ["Date"]=>
    string(29) "Tue, 20 Jun 2017 11:20:14 GMT"
    ["Content-Type"]=>
    string(16) "application/json"
    ["Transfer-Encoding"]=>
    string(7) "chunked"
    ["Connection"]=>
    string(10) "keep-alive"
    ["X-Rate-Limit-Limit"]=>
    string(3) "600"
    ["X-Rate-Limit-Remaining"]=>
    string(3) "597"
    ["X-Rate-Limit-Reset"]=>
    string(2) "53"
    ["X-JPush-MsgId"]=>
    string(17) "24769798198999500"
  }
}


本文出自 “为了以后” 博客,谢绝转载!

TP5中使用极光推送3.5.12

标签:php   tp5使用极光推送   thinkphp5-jpush3.5.12   

原文地址:http://tengteng412.blog.51cto.com/4751263/1940423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!