push.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. $data = [
  22. 'cid' => is_array($cids) ? $cids : [$cids],
  23. 'title' => $title,
  24. 'content' => $content,
  25. 'payload' => $payload,
  26. 'force_notification' => true, // true: 无论是离线推送还是在线推送,都自创建通知栏消息
  27. 'sign' => $sign
  28. ];
  29. $ch = curl_init($this->url);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. curl_setopt($ch, CURLOPT_POST, true);
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  33. 'Content-Type: application/json'
  34. ]);
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  36. $res = curl_exec($ch);
  37. curl_close($ch);
  38. return json_decode($res, true);
  39. }
  40. }