新闻中心

记录团队成长点滴以及对技术、理念的探索,同时我们乐于分享!

在Laravel中集成腾讯云 移动(APP)推送(TPNS)

2020-08-20 15:56:50 分类:技术学堂

腾讯云推送的文档,没有 php 的 sdk,只好自己写了


<?php

namespace App\Utility;

use App\Models\User;

class Push
{
//腾讯的推送服务,安卓和ios两个端对应是两个应用,当后端推送时,无法知道用户是ios设备还是安卓设备,只能两端都发送一下
    private $android_access_id = '';
    private $android_secret_key = '';
    private $ios_access_id = '';
    private $ios_secret_key = '';
    private $url = 'https://api.tpns.tencent.com/v3/push/app';
    //签名
    public function sign($body,$time, $type = 'android')
    {
        if ($type == 'android') {
            $str = $time . $this->android_access_id . $body;
            return base64_encode(hash_hmac('sha256', $str, $this->android_secret_key));
        } else {
            $str = $time . $this->ios_access_id . $body;
            return base64_encode(hash_hmac('sha256', $str, $this->ios_secret_key));
        }

    }

    /**
     * @param $user_id
     * @param $title
     * @param $content
     * @param array $params
     * @return bool|mixed
     * 发送消息至安卓设备
     */
   public function sendToAndroid($user_id, $title, $content, $params = [])
   {
        $user = User::find($user_id);
        if (!$user || !$user->tpns_token) {
        //获取用户推送的唯一标示
            return false;
        }
       $message = [
           'title'  => $title,
           'content'    =>  $content,
       ];
       if (count($params)) {
           $message['android']['custom_content'] = json_encode($params);
        }
        $data = [
            'audience_type' =>  'token',
            'token_list'    =>  [$user->tpns_token],
            'message_type' =>  'notify',
            'message' =>  $message,
        ];
        $time = time();
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $sign = $this->sign($data, $time, 'android');
        $header = [
            'AccessId:'.$this->android_access_id,
            'TimeStamp:'.$time,
            'Sign:'.$sign
        ];
        return httpRequest($this->url,$data,false,$header );
   }

    /**
     * @param $user_id
     * @param $title
     * @param $content
     * @param array $params
     * @return bool|mixed
     * 发送推送至ios设备
     */
    public function sendToIos($user_id, $title, $content, $params = [])
    {
        $user = User::find($user_id);
        if (!$user || !$user->tpns_token) {
            return false;
        }
        $message = [
            'title'  => $title,
            'content'    =>  $content,
            'ios'   =>  [
                'aps' => [
                    'badge_type'    =>  1,
                ]
            ]
        ];
        if (count($params)) {
            $message['ios']['custom_content'] = json_encode($params);
        }
        $data = [
            'audience_type' =>  'token',
            'token_list'    =>  [$user->tpns_token],
            'message_type' =>  'notify',
            'message' =>  $message,
            'environment'   =>  'dev'//product
        ];
        $time = time();
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $sign = $this->sign($data, $time, 'ios');
        $header = [
            'AccessId:'.$this->ios_access_id,
            'TimeStamp:'.$time,
            'Sign:'.$sign
        ];
        return httpRequest($this->url,$data,false,$header );
    }

}


使用如下:

$push = new \App\Utility\Push();
$push->sendToIos($this->user_id,$this->title,$this->content,$this->params);
$push->sendToAndroid($this->user_id,$this->title,$this->content,$this->params);