华为push的PHP语言的SDKdemo

直接上代码吧,照着官方文档调一下参数就行,简版测试通过。


class HuaweiNoti
{
    protected $tokenExpiredTime        = NULL;
    protected $accessToken = NULL;

   function __construct()
   {
      $this->appId = '******';
      $this->appSecret = '******';
      $this->tokenUrl = "******"; //获取认证Token的URL
        $this->apiUrl = "******"; //应用级消息下发API
        $this->timestamp = strval(time());
   }

   // 获取token
    function onToken()
    {
        $res = http_post(
            'https://login.cloud.huawei.com/oauth2/v2/token',
            [
                'grant_type' => 'client_credentials',
                'client_secret' => $this->appSecret,
                'client_id' => $this->appId,
            ]
        );

        $resArray = json_decode($res, true);

        if(isset($resArray['access_token']) && isset($resArray['expires_in'])){
            $huaweiToken = $resArray['access_token'];
        }
        return $huaweiToken;
    }

   //单播消息
   function sendAndroidUnicast($device_tokens,$title,$text,$param = [])
   {
        $huaweiToken = $this->onToken();

        try {
            $body = array();//仅通知栏消息需要设置标题和内容,透传消息key和value为用户自定义
            $body['title'] = $title;//消息标题
            $body['content'] = $text;//消息标题

            $para = array();
            $para['appPkgName'] = '';//定义需要打开的appPkgName

            $action = array();

            $action['param'] = $para;//消息点击动作参数
            $action['type'] = 3;//类型3为打开APP,其他行为请参考接口文档设置
            $msg = array();

            $msg['action'] = $action;//消息点击动作
            $msg['type'] = 3;//3: 通知栏消息,异步透传消息请根据接口文档设置
            $msg['body'] = $body;//通知栏消息body内容
            $ext = array();//扩展信息,含BI消息统计,特定展示风格,消息折叠。
            $ext['biTag'] = 'Trump';//设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
            $ext['icon'] = "";//自定义推送消息在通知栏的图标,value为一个公网可以访问的URL

            $hps = array();//华为PUSH消息总结构体
            $hps['msg'] = $msg;
            $hps['ext'] = $ext;

            $payload = array();
            $payload['hps'] = $hps;

            $res = http_post(
                'https://api.push.hicloud.com/pushsend.do?nsp_ctx=' . urlencode('{"ver":"1", "appId":"******"}'),
                [
                    'access_token' => $huaweiToken,
                    'nsp_svc' => 'openpush.message.api.send',
                    'nsp_ts' => (int)time(),
                    'device_token_list' => json_encode([$device_tokens]),
                    'payload' => json_encode($payload),
                ]
            );
            //var_dump(json_decode($res, true));//查看结果

        } catch (Exception $e) {
         print("Caught exception: " . $e->getMessage());
      }
   }}

注:http_post 自己封装一个http_curl的方法就行。

/**
 * 发送HTTP请求方法
 * @param  string $url    请求URL
 * @param  array  $postData 请求参数
 * @param  array  $header 请求头
 * @return array  $data   响应数据
 */
function http_post($url, $postData = [], $formUrlencoded = true)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $header = ['Content-Type: application/x-www-form-urlencoded; charset=utf-8'];

    if ($header) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    if ($postData) {
        curl_setopt($ch, CURLOPT_POST, true);
        //如果不用http_build_query你就会踩到坑的,你可以试试
        if($formUrlencoded){
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
        }else{
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }
    }

    $response  = curl_exec($ch);

    if ($errno = curl_errno($ch)) {
        $error = curl_error($ch);
        $this->errmsg = $error;
        $this->errno = $errno;
        curl_close($ch);

        return false;
    }
    curl_close($ch);

    return $response ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值