HwPushCommon.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Libs\Push\HuaWei;
  3. use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
  4. use App\Libs\Push\HuaWei\Admin\Application;
  5. use App\Libs\Push\HuaWei\Admin\Msg\Android\AndroidConfig;
  6. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppConfig;
  7. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppPushBody;
  8. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppRingTone;
  9. use App\Libs\Push\HuaWei\Admin\Msg\PushMessage;
  10. /**
  11. * 快应用
  12. * Class FastAppPushCommon
  13. * @package App\Libs\Push\HuaWei
  14. */
  15. class HwPushCommon
  16. {
  17. private $_app_id;
  18. private $_app_secret;
  19. private $_token = [];
  20. private $_topic = '';
  21. private $_fast_app_target = 2;
  22. private $_big_tag = 'app';
  23. public function __construct($appId, $appSecret)
  24. {
  25. $this->_app_id = $appId;
  26. $this->_app_secret = $appSecret;
  27. }
  28. /**
  29. * 设置token
  30. * @param $tokenArr
  31. */
  32. public function setToken($tokenArr)
  33. {
  34. $this->_token = $tokenArr;
  35. }
  36. /**
  37. * 设置topic
  38. * @param $topic
  39. */
  40. public function setTopic($topic)
  41. {
  42. $this->_topic = $topic;
  43. }
  44. /**
  45. * 设置开发态和生产态,1:开发态, 2:生产态
  46. * @param $target
  47. */
  48. public function setFastAppTarget($target)
  49. {
  50. $this->_fast_app_target = $target;
  51. }
  52. /**
  53. * 设置bigTag
  54. * @param $bigTag
  55. */
  56. public function setBigTag($bigTag)
  57. {
  58. $this->_big_tag = $bigTag;
  59. }
  60. /**
  61. * 发送通知消息
  62. * @param $title
  63. * @param $desc
  64. * @param $pageUrl
  65. * @return mixed|null
  66. */
  67. public function sendPushMessage($title, $desc, $pageUrl)
  68. {
  69. // 组装发送数据
  70. $data = $this->createFastAppConfigNotificationData($title, $desc, $pageUrl);
  71. $message = $this->createFastAppMsg($data->getFields());
  72. # 创建app
  73. $application = $this->createApplication();
  74. # 发送消息
  75. return $application->push_send_msg($message->getFields());
  76. }
  77. /**
  78. * 查询push_token信息
  79. * @param $pushToken
  80. * @return mixed|null
  81. */
  82. public function queryPushToken($pushToken)
  83. {
  84. # 创建app
  85. $application = $this->createApplication();
  86. return $application->query_push_token($pushToken);
  87. }
  88. /**
  89. * 主题订阅
  90. * @param $topic
  91. * @param $tokenArr
  92. * @return bool|mixed|string|null
  93. */
  94. public function subscribeTopic($topic, $tokenArr)
  95. {
  96. # 创建app
  97. $application = $this->createApplication();
  98. return $application->subscribe_topic($topic, $tokenArr);
  99. }
  100. /**
  101. * 主题退订
  102. * @param $topic
  103. * @param $tokenArr
  104. * @return bool|mixed|string|null
  105. */
  106. public function unSubscribeTopic($topic, $tokenArr)
  107. {
  108. # 创建app
  109. $application = $this->createApplication();
  110. return $application->unsubscribe_topic($topic, $tokenArr);
  111. }
  112. /**
  113. * 主题退订
  114. * @param $token
  115. * @return bool|mixed|string|null
  116. */
  117. public function subscribeList($token)
  118. {
  119. # 创建app
  120. $application = $this->createApplication();
  121. return $application->topic_list($token);
  122. }
  123. /**
  124. * @param $data
  125. * @param array $tokenArr
  126. * @param string $topic
  127. * @return PushMessage
  128. */
  129. private function createFastAppMsg($data): PushMessage
  130. {
  131. $message = new PushMessage();
  132. $message->data($data);
  133. $message->android($this->createAndroidConfig()->getFields());
  134. if ($this->_topic) {
  135. // 按照Topic向订阅了本topic的用户推消息
  136. $message->topic($this->_topic);
  137. } else {
  138. // 按照Token向目标用户推消息
  139. $message->token($this->_token);
  140. }
  141. $message->buildFields();
  142. return $message;
  143. }
  144. /**
  145. * @param $title
  146. * @param $desc
  147. * @param string $pageUrl
  148. * @param int $pushType
  149. * @param array $params
  150. * @return InstanceAppConfig
  151. */
  152. private function createFastAppConfigNotificationData($title, $desc, $pageUrl = '/', $pushType = 0, $params = []): InstanceAppConfig
  153. {
  154. // 设置推送类型,0为通知栏消息
  155. $instanceAppConfig = new InstanceAppConfig();
  156. $instanceAppConfig->pushtype($pushType);
  157. //快应用推送消息体
  158. $instanceAppPushBody = new InstanceAppPushBody();
  159. $instanceAppPushBody->title($title);
  160. $instanceAppPushBody->description($desc);
  161. $instanceAppPushBody->page($pageUrl);
  162. $instanceAppPushBody->params($params);
  163. // 设置是否震动和显示呼吸灯
  164. $instanceAppRingTone = new InstanceAppRingTone();
  165. $instanceAppRingTone->breathLight(true);
  166. $instanceAppRingTone->vibration(true);
  167. $instanceAppRingTone->buildFields();
  168. $instanceAppPushBody->ringtone($instanceAppRingTone->getFields());
  169. $instanceAppPushBody->buildFields();
  170. $instanceAppConfig->pushbody($instanceAppPushBody->getFields());
  171. $instanceAppConfig->buildFields();
  172. return $instanceAppConfig;
  173. }
  174. /**
  175. * @return AndroidConfig
  176. */
  177. private function createAndroidConfig(): AndroidConfig
  178. {
  179. $android_config = new AndroidConfig();
  180. $android_config->collapse_key(-1);
  181. $android_config->urgency(AndroidConfigDeliveryPriority::PRIORITY_HIGH);
  182. $android_config->bi_tag('app');
  183. $android_config->fast_app_target($this->_fast_app_target);
  184. $android_config->buildFields();
  185. return $android_config;
  186. }
  187. /**
  188. * @return Application
  189. */
  190. private function createApplication(): Application
  191. {
  192. return new Application($this->_app_id, $this->_app_secret);
  193. }
  194. }