push.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace common\components;
  3. class push
  4. {
  5. private $ghsUrl = 'https://fc-mp-42d1a66f-2aec-4fc5-9d92-4d176bf3d337.next.bspapp.com/ghs/push';
  6. private $hdUrl = 'https://fc-mp-027cce01-d39a-4ed2-8b09-1f4ff43ec945.next.bspapp.com/hd/push';
  7. private $url = '';
  8. private $secret = 'RIVATE_KEY_S^@VJDy7'; // 👈 与云函数中保持一致
  9. public function __construct($client) {
  10. if ($client == 'ghs') {
  11. $this->url = $this->ghsUrl;
  12. } else if ($client == 'hd') {
  13. $this->url = $this->hdUrl;
  14. } else {
  15. throw new \Exception('client not found');
  16. }
  17. }
  18. public function pushByCloud($cids, $title, $content, $payload = [])
  19. {
  20. $sign = md5($title . $content . $this->secret);
  21. $optionsArr = [ "android" =>
  22. [
  23. // "XM" => [
  24. // "/extra.channel_id" => "" //小米后台申请的通知类别id
  25. // ],
  26. "HW" => [
  27. "/message/android/category" => "EXPRESS"
  28. ]
  29. ]
  30. ];
  31. $data = [
  32. 'cid' => is_array($cids) ? $cids : [$cids],
  33. 'title' => $title,
  34. 'content' => $content,
  35. 'payload' => $payload,
  36. 'force_notification' => true, // true: 无论是离线推送还是在线推送,都自创建通知栏消息
  37. 'sign' => $sign,
  38. 'options' => $optionsArr
  39. ];
  40. $ch = curl_init($this->url);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42. curl_setopt($ch, CURLOPT_POST, true);
  43. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  44. 'Content-Type: application/json'
  45. ]);
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  47. $res = curl_exec($ch);
  48. curl_close($ch);
  49. return json_decode($res, true);
  50. }
  51. }