123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- namespace App\Libs\Push\HuaWei;
- use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
- use App\Libs\Push\HuaWei\Admin\Application;
- use App\Libs\Push\HuaWei\Admin\Msg\Android\AndroidConfig;
- use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppConfig;
- use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppPushBody;
- use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppRingTone;
- use App\Libs\Push\HuaWei\Admin\Msg\PushMessage;
- /**
- * 快应用
- * Class FastAppPushCommon
- * @package App\Libs\Push\HuaWei
- */
- class HwPushCommon
- {
- private $_app_id;
- private $_app_secret;
- private $_token = [];
- private $_topic = '';
- private $_fast_app_target = 2;
- private $_big_tag = 'app';
- public function __construct($appId, $appSecret)
- {
- $this->_app_id = $appId;
- $this->_app_secret = $appSecret;
- }
- /**
- * 设置token
- * @param $tokenArr
- */
- public function setToken($tokenArr)
- {
- $this->_token = $tokenArr;
- }
- /**
- * 设置topic
- * @param $topic
- */
- public function setTopic($topic)
- {
- $this->_topic = $topic;
- }
- /**
- * 设置开发态和生产态,1:开发态, 2:生产态
- * @param $target
- */
- public function setFastAppTarget($target)
- {
- $this->_fast_app_target = $target;
- }
- /**
- * 设置bigTag
- * @param $bigTag
- */
- public function setBigTag($bigTag)
- {
- $this->_big_tag = $bigTag;
- }
- /**
- * 发送通知消息
- * @param $title
- * @param $desc
- * @param $pageUrl
- * @param array $params
- * @return bool|mixed|string|null
- */
- public function sendPushMessage($title, $desc, $pageUrl, $params = [])
- {
- // 组装发送数据
- $data = $this->createFastAppConfigNotificationData($title, $desc, $pageUrl, 0, $params);
- $message = $this->createFastAppMsg($data->getFields());
- # 创建app
- $application = $this->createApplication();
- # 发送消息
- return $application->push_send_msg($message->getFields());
- }
- /**
- * 查询push_token信息
- * @param $pushToken
- * @return mixed|null
- */
- public function queryPushToken($pushToken)
- {
- # 创建app
- $application = $this->createApplication();
- return $application->query_push_token($pushToken);
- }
- /**
- * 主题订阅
- * @param $topic
- * @param $tokenArr
- * @return bool|mixed|string|null
- */
- public function subscribeTopic($topic, $tokenArr)
- {
- # 创建app
- $application = $this->createApplication();
- return $application->subscribe_topic($topic, $tokenArr);
- }
- /**
- * 主题退订
- * @param $topic
- * @param $tokenArr
- * @return bool|mixed|string|null
- */
- public function unSubscribeTopic($topic, $tokenArr)
- {
- # 创建app
- $application = $this->createApplication();
- return $application->unsubscribe_topic($topic, $tokenArr);
- }
- /**
- * 主题退订
- * @param $token
- * @return bool|mixed|string|null
- */
- public function subscribeList($token)
- {
- # 创建app
- $application = $this->createApplication();
- return $application->topic_list($token);
- }
- /**
- * @param $data
- * @param array $tokenArr
- * @param string $topic
- * @return PushMessage
- */
- private function createFastAppMsg($data): PushMessage
- {
- $message = new PushMessage();
- $message->data($data);
- $message->android($this->createAndroidConfig()->getFields());
- if ($this->_topic) {
- // 按照Topic向订阅了本topic的用户推消息
- $message->topic($this->_topic);
- } else {
- // 按照Token向目标用户推消息
- $message->token($this->_token);
- }
- $message->buildFields();
- return $message;
- }
- /**
- * @param $title
- * @param $desc
- * @param string $pageUrl
- * @param int $pushType
- * @param array $params
- * @return InstanceAppConfig
- */
- private function createFastAppConfigNotificationData($title, $desc, $pageUrl = '/', $pushType = 0, $params = []): InstanceAppConfig
- {
- // 设置推送类型,0为通知栏消息
- $instanceAppConfig = new InstanceAppConfig();
- $instanceAppConfig->pushtype($pushType);
- //快应用推送消息体
- $instanceAppPushBody = new InstanceAppPushBody();
- $instanceAppPushBody->title($title);
- $instanceAppPushBody->description($desc);
- $instanceAppPushBody->page($pageUrl);
- $instanceAppPushBody->params($params);
- // 设置是否震动和显示呼吸灯
- $instanceAppRingTone = new InstanceAppRingTone();
- $instanceAppRingTone->breathLight(true);
- $instanceAppRingTone->vibration(true);
- $instanceAppRingTone->buildFields();
- $instanceAppPushBody->ringtone($instanceAppRingTone->getFields());
- $instanceAppPushBody->buildFields();
- $instanceAppConfig->pushbody($instanceAppPushBody->getFields());
- $instanceAppConfig->buildFields();
- return $instanceAppConfig;
- }
- /**
- * @return AndroidConfig
- */
- private function createAndroidConfig(): AndroidConfig
- {
- $android_config = new AndroidConfig();
- $android_config->collapse_key(-1);
- $android_config->urgency(AndroidConfigDeliveryPriority::PRIORITY_HIGH);
- $android_config->bi_tag('app');
- $android_config->fast_app_target($this->_fast_app_target);
- $android_config->buildFields();
- return $android_config;
- }
- /**
- * @return Application
- */
- private function createApplication(): Application
- {
- return new Application($this->_app_id, $this->_app_secret);
- }
- }
|