|
|
<?php
|
|
|
|
|
|
namespace getui\push;
|
|
|
|
|
|
use getui\Base;
|
|
|
|
|
|
class Push extends Base
|
|
|
{
|
|
|
/**
|
|
|
* 单推-执行cid单推(目前厂商渠道建议使用的通知+intent方式 在用)
|
|
|
* @param string $client_id CID
|
|
|
* @param string $request_id 请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
|
|
|
* @param string $title 通知消息标题,长度 ≤ 50
|
|
|
* @param string $content 通知消息内容,长度 ≤ 256
|
|
|
* @param string $package_name 包名
|
|
|
* @param string $payload 增加自定义的数据
|
|
|
* @date 2021-12-23
|
|
|
*/
|
|
|
function toSinglePushByCid($client_id, $request_id, $title, $content, $package_name, $payload = 'test')
|
|
|
{
|
|
|
//创建API,APPID等配置参考 环境要求 进行获取
|
|
|
$api = new \GTClient($this->url, $this->config['app_key'], $this->config['app_id'], $this->config['master_secret']);
|
|
|
|
|
|
//----------在线个推透传消息----------
|
|
|
//纯透传消息内容,安卓和iOS均支持,长度 ≤ 3072
|
|
|
$transmission = [
|
|
|
'title' => $title,
|
|
|
'content' => $content,
|
|
|
'payload' => $payload
|
|
|
];
|
|
|
//长度 ≤ 4096; 离线厂商通知
|
|
|
$intent = "intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=$package_name/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=$title;S.content=$content;S.payload=$payload;end";
|
|
|
|
|
|
// #设置推送参数
|
|
|
$push = new \GTPushRequest();
|
|
|
$push->setRequestId($request_id);
|
|
|
|
|
|
// ##设置个推推送参数
|
|
|
$push_message = new \GTPushMessage();
|
|
|
|
|
|
$push_message->setTransmission(json_encode($transmission)); //notification通知消息内容,仅支持安卓系统,iOS系统不展示个推通知消息,与transmission、revoke三选一,都填写时报错
|
|
|
$push->setPushMessage($push_message);//在线个推透传(安卓和IOS都支持)
|
|
|
|
|
|
// ##设置厂商推送参数
|
|
|
$push_channel = new \GTPushChannel(); //厂商通道消息内容
|
|
|
|
|
|
// ###厂商参数-安卓参数
|
|
|
$android = new \GTAndroid(); // android通道推送消息内容
|
|
|
$ups = new \GTUps(); //android厂商通道推送消息内容
|
|
|
|
|
|
$notify = new \GTNotification();
|
|
|
$notify->setTitle($title);
|
|
|
$notify->setBody($content);
|
|
|
$notify->setClickType('intent');//点击通知后续动作,目前支持以下后续动作:
|
|
|
$notify->setIntent($intent);
|
|
|
|
|
|
$ups->setNotification($notify);
|
|
|
$android->setUps($ups);
|
|
|
$push_channel->setAndroid($android);
|
|
|
|
|
|
// ###厂商参数-ios参数
|
|
|
$ios = new \GTIos();
|
|
|
$aps = new \GTAps();
|
|
|
|
|
|
$ios->setType('notify');
|
|
|
$ios->setPayload($payload);
|
|
|
$ios->setAutoBadge('+1');
|
|
|
|
|
|
$alert = new \GTAlert();
|
|
|
$alert->setTitle($title);
|
|
|
$alert->setBody($content);
|
|
|
|
|
|
$aps->setAlert($alert);
|
|
|
$ios->setAps($aps);
|
|
|
$push_channel->setIos($ios);
|
|
|
|
|
|
$push->setPushChannel($push_channel);
|
|
|
|
|
|
$push->setCid($client_id); //audience 必填 推送目标用户 audience['cid']
|
|
|
// dump($push->getApiParam());
|
|
|
// dump($this->object_to_json($push));
|
|
|
|
|
|
//处理返回结果
|
|
|
$result = $api->pushApi()->pushToSingleByCid($push);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量推-创建消息(目前厂商渠道建议使用的通知+intent方式 在用)
|
|
|
* @param string $client_id CID
|
|
|
* @param string $request_id 请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
|
|
|
* @param string $title 通知消息标题,长度 ≤ 50
|
|
|
* @param string $content 通知消息内容,长度 ≤ 256
|
|
|
* @param string $package_name 包名
|
|
|
* @param string $payload 增加自定义的数据
|
|
|
* @date 2022-04-26
|
|
|
*/
|
|
|
function toListCreatePush($request_id, $title, $content, $package_name, $payload = 'test')
|
|
|
{
|
|
|
//创建API,APPID等配置参考 环境要求 进行获取
|
|
|
$api = new \GTClient($this->url, $this->config['app_key'], $this->config['app_id'], $this->config['master_secret']);
|
|
|
|
|
|
// 透传消息
|
|
|
$transmission = [
|
|
|
'title' => $title,
|
|
|
'content' => $content,
|
|
|
'payload' => $payload
|
|
|
];
|
|
|
//长度 ≤ 4096; 离线厂商通知
|
|
|
$intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=$package_name/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=$title;S.content=$content;S.payload=$payload;end";
|
|
|
|
|
|
|
|
|
// #设置推送参数
|
|
|
$push = new \GTPushRequest();
|
|
|
$push->setRequestId($request_id);
|
|
|
|
|
|
// ##设置个推推送参数
|
|
|
$push_message = new \GTPushMessage();
|
|
|
$push_message->setTransmission(json_encode($transmission)); //纯透传消息内容,安卓和iOS均支持,长度 ≤ 3072 在线个推透传
|
|
|
$push->setPushMessage($push_message);//在线个推透传(安卓和IOS都支持)
|
|
|
|
|
|
|
|
|
// ##设置厂商推送参数
|
|
|
$push_channel = new \GTPushChannel(); //厂商通道消息内容
|
|
|
|
|
|
// ###厂商参数-android参数
|
|
|
$android = new \GTAndroid(); // android通道推送消息内容
|
|
|
$ups = new \GTUps(); //android厂商通道推送消息内容
|
|
|
$notify = new \GTNotification();
|
|
|
$notify->setTitle($title);
|
|
|
$notify->setBody($content);
|
|
|
$notify->setClickType('intent');//点击通知后续动作,目前支持以下后续动作:
|
|
|
$notify->setIntent($intent);
|
|
|
$ups->setNotification($notify);
|
|
|
$android->setUps($ups);
|
|
|
$push_channel->setAndroid($android);
|
|
|
|
|
|
// ###厂商参数-ios参数
|
|
|
$ios = new \GTIos();
|
|
|
$aps = new \GTAps();
|
|
|
$ios->setType('notify');
|
|
|
$ios->setPayload($payload);
|
|
|
$ios->setAutoBadge('+1');
|
|
|
$alert = new \GTAlert();
|
|
|
$alert->setTitle($title);
|
|
|
$alert->setBody($content);
|
|
|
$aps->setAlert($alert);
|
|
|
$ios->setAps($aps);
|
|
|
$push_channel->setIos($ios);
|
|
|
|
|
|
|
|
|
$push->setPushChannel($push_channel);
|
|
|
|
|
|
//处理返回结果
|
|
|
$result = $api->pushApi()->createListMsg($push);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量推-执行cid批量推(目前厂商渠道建议使用的通知+intent方式 在用)
|
|
|
* @param array $client_ids CID
|
|
|
* @param string $task_id 任务ID
|
|
|
* @date 2022-04-27
|
|
|
*/
|
|
|
function toListPushByCid($client_ids, $task_id)
|
|
|
{
|
|
|
//创建API,APPID等配置参考 环境要求 进行获取
|
|
|
$api = new \GTClient($this->url, $this->config['app_key'], $this->config['app_id'], $this->config['master_secret']);
|
|
|
|
|
|
$array = [
|
|
|
'audience' => [
|
|
|
'cid' => $client_ids
|
|
|
],
|
|
|
'taskid' => $task_id
|
|
|
];
|
|
|
|
|
|
// #设置推送参数
|
|
|
$audience = new \GTAudienceRequest();
|
|
|
$audience->setIsAsync(false); //是否异步推送,true是异步,false同步。异步推送不会返回data详情
|
|
|
$audience->setTaskid($task_id);
|
|
|
$audience->setCidList($client_ids);
|
|
|
|
|
|
|
|
|
//处理返回结果
|
|
|
$result = $api->pushApi()->pushListByCid($audience);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量推-执行群推(目前厂商渠道建议使用的通知+intent方式 在用)
|
|
|
* @param array $client_ids CID
|
|
|
* @param string $task_id 任务ID
|
|
|
* @date 2022-04-27
|
|
|
*/
|
|
|
function toAppPush($request_id, $title, $content, $package_name, $payload = 'test')
|
|
|
{
|
|
|
//创建API,APPID等配置参考 环境要求 进行获取
|
|
|
$api = new \GTClient($this->url, $this->config['app_key'], $this->config['app_id'], $this->config['master_secret']);
|
|
|
|
|
|
// 透传消息
|
|
|
$transmission = [
|
|
|
'title' => $title,
|
|
|
'content' => $content,
|
|
|
'payload' => $payload
|
|
|
];
|
|
|
//长度 ≤ 4096; 离线厂商通知
|
|
|
$intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=$package_name/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=$title;S.content=$content;S.payload=$payload;end";
|
|
|
|
|
|
|
|
|
// #设置推送参数
|
|
|
$push = new \GTPushRequest();
|
|
|
$push->setRequestId($request_id);
|
|
|
|
|
|
// ##设置个推推送参数
|
|
|
$push_message = new \GTPushMessage();
|
|
|
$push_message->setTransmission(json_encode($transmission)); //纯透传消息内容,安卓和iOS均支持,长度 ≤ 3072 在线个推透传
|
|
|
$push->setPushMessage($push_message);//在线个推透传(安卓和IOS都支持)
|
|
|
|
|
|
|
|
|
// ##设置厂商推送参数
|
|
|
$push_channel = new \GTPushChannel(); //厂商通道消息内容
|
|
|
|
|
|
// ###厂商参数-android参数
|
|
|
$android = new \GTAndroid(); // android通道推送消息内容
|
|
|
$ups = new \GTUps(); //android厂商通道推送消息内容
|
|
|
$notify = new \GTNotification();
|
|
|
$notify->setTitle($title);
|
|
|
$notify->setBody($content);
|
|
|
$notify->setClickType('intent');//点击通知后续动作,目前支持以下后续动作:
|
|
|
$notify->setIntent($intent);
|
|
|
$ups->setNotification($notify);
|
|
|
$android->setUps($ups);
|
|
|
$push_channel->setAndroid($android);
|
|
|
|
|
|
// ###厂商参数-ios参数
|
|
|
$ios = new \GTIos();
|
|
|
$aps = new \GTAps();
|
|
|
$ios->setType('notify');
|
|
|
$ios->setPayload($payload);
|
|
|
$ios->setAutoBadge('+1');
|
|
|
$alert = new \GTAlert();
|
|
|
$alert->setTitle($title);
|
|
|
$alert->setBody($content);
|
|
|
$aps->setAlert($alert);
|
|
|
$ios->setAps($aps);
|
|
|
$push_channel->setIos($ios);
|
|
|
|
|
|
|
|
|
$push->setPushChannel($push_channel);
|
|
|
|
|
|
//处理返回结果
|
|
|
$result = $api->pushApi()->pushAll($push);
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//***************************************************
|
|
|
|
|
|
/**
|
|
|
* @param $obj
|
|
|
* @return array
|
|
|
*/
|
|
|
//convert object to array
|
|
|
function object_to_array($obj)
|
|
|
{
|
|
|
if (is_array($obj)) {
|
|
|
return $obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
|
|
|
|
|
|
$arr = [];
|
|
|
foreach ($_arr as $key => $val) {
|
|
|
$val = (is_array($val)) || is_object($val) ? object_to_array($val) : $val;
|
|
|
|
|
|
$arr[$key] = $val;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $arr;
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 把对象转化为json
|
|
|
*/
|
|
|
|
|
|
function object_to_json($obj)
|
|
|
{
|
|
|
$arr2 = $this->object_to_array($obj);//先把对象转化为数组
|
|
|
|
|
|
return json_encode($arr2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
} |