HwPushCommon.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * @param array $params
  66. * @return bool|mixed|string|null
  67. */
  68. public function sendPushMessage($title, $desc, $pageUrl, $params = [])
  69. {
  70. // 组装发送数据
  71. $data = $this->createFastAppConfigNotificationData($title, $desc, $pageUrl, 0, $params);
  72. $message = $this->createFastAppMsg($data->getFields());
  73. # 创建app
  74. $application = $this->createApplication();
  75. # 发送消息
  76. return $application->push_send_msg($message->getFields());
  77. }
  78. /**
  79. * 查询push_token信息
  80. * @param $pushToken
  81. * @return mixed|null
  82. */
  83. public function queryPushToken($pushToken)
  84. {
  85. # 创建app
  86. $application = $this->createApplication();
  87. return $application->query_push_token($pushToken);
  88. }
  89. /**
  90. * 主题订阅
  91. * @param $topic
  92. * @param $tokenArr
  93. * @return bool|mixed|string|null
  94. */
  95. public function subscribeTopic($topic, $tokenArr)
  96. {
  97. # 创建app
  98. $application = $this->createApplication();
  99. return $application->subscribe_topic($topic, $tokenArr);
  100. }
  101. /**
  102. * 主题退订
  103. * @param $topic
  104. * @param $tokenArr
  105. * @return bool|mixed|string|null
  106. */
  107. public function unSubscribeTopic($topic, $tokenArr)
  108. {
  109. # 创建app
  110. $application = $this->createApplication();
  111. return $application->unsubscribe_topic($topic, $tokenArr);
  112. }
  113. /**
  114. * 主题退订
  115. * @param $token
  116. * @return bool|mixed|string|null
  117. */
  118. public function subscribeList($token)
  119. {
  120. # 创建app
  121. $application = $this->createApplication();
  122. return $application->topic_list($token);
  123. }
  124. /**
  125. * @param $data
  126. * @param array $tokenArr
  127. * @param string $topic
  128. * @return PushMessage
  129. */
  130. private function createFastAppMsg($data): PushMessage
  131. {
  132. $message = new PushMessage();
  133. $message->data($data);
  134. $message->android($this->createAndroidConfig()->getFields());
  135. if ($this->_topic) {
  136. // 按照Topic向订阅了本topic的用户推消息
  137. $message->topic($this->_topic);
  138. } else {
  139. // 按照Token向目标用户推消息
  140. $message->token($this->_token);
  141. }
  142. $message->buildFields();
  143. return $message;
  144. }
  145. /**
  146. * @param $title
  147. * @param $desc
  148. * @param string $pageUrl
  149. * @param int $pushType
  150. * @param array $params
  151. * @return InstanceAppConfig
  152. */
  153. private function createFastAppConfigNotificationData($title, $desc, $pageUrl = '/', $pushType = 0, $params = []): InstanceAppConfig
  154. {
  155. // 设置推送类型,0为通知栏消息
  156. $instanceAppConfig = new InstanceAppConfig();
  157. $instanceAppConfig->pushtype($pushType);
  158. //快应用推送消息体
  159. $instanceAppPushBody = new InstanceAppPushBody();
  160. $instanceAppPushBody->title($title);
  161. $instanceAppPushBody->description($desc);
  162. $instanceAppPushBody->page($pageUrl);
  163. $instanceAppPushBody->params($params);
  164. // 设置是否震动和显示呼吸灯
  165. $instanceAppRingTone = new InstanceAppRingTone();
  166. $instanceAppRingTone->breathLight(true);
  167. $instanceAppRingTone->vibration(true);
  168. $instanceAppRingTone->buildFields();
  169. $instanceAppPushBody->ringtone($instanceAppRingTone->getFields());
  170. $instanceAppPushBody->buildFields();
  171. $instanceAppConfig->pushbody($instanceAppPushBody->getFields());
  172. $instanceAppConfig->buildFields();
  173. return $instanceAppConfig;
  174. }
  175. /**
  176. * @return AndroidConfig
  177. */
  178. private function createAndroidConfig(): AndroidConfig
  179. {
  180. $android_config = new AndroidConfig();
  181. $android_config->collapse_key(-1);
  182. $android_config->urgency(AndroidConfigDeliveryPriority::PRIORITY_HIGH);
  183. $android_config->bi_tag('app');
  184. $android_config->fast_app_target($this->_fast_app_target);
  185. $android_config->buildFields();
  186. return $android_config;
  187. }
  188. /**
  189. * @return Application
  190. */
  191. private function createApplication(): Application
  192. {
  193. return new Application($this->_app_id, $this->_app_secret);
  194. }
  195. }