JPushService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Log;
  4. class JPushService
  5. {
  6. protected static $client = null;
  7. //推送类型
  8. const PUSH_TYPE_ALL = 1;
  9. const PUSH_TYPE_TAG = 2;
  10. const PUSH_TYPE_ALIAS = 3;
  11. const PUSH_TYPE_REG_ID = 4;
  12. private function __construct()
  13. {
  14. }
  15. private function __clone()
  16. {
  17. }
  18. /**
  19. * 获取实例
  20. */
  21. public static function getInstance()
  22. {
  23. if (!self::$client) {
  24. self::$client = new JPush(config('jpush.app_key'), config('jpush.master_secret'), null);
  25. }
  26. return self::$client;
  27. }
  28. /**
  29. * 给android或ios推送消息
  30. */
  31. public static function pushNotify($params)
  32. {
  33. //推送平台
  34. $platform = $params['platform'] ?? 'all';
  35. //推送标题
  36. $title = $params['title'] ?? '';
  37. //推送内容
  38. $content = $params['content'] ?? '';
  39. //通知栏样式ID
  40. $builder_id = $params['builder_id'] ?? 0;
  41. //附加字段
  42. $extras = $params['extras'] ?? '';
  43. //推送类型
  44. $type = $params['type'] ?? '';
  45. //推送目标(注册ID)
  46. $reg_id = $params['reg_id'] ?? '';
  47. //推送目标(标签)
  48. $tag = $params['tag'] ?? '';
  49. //推送目标(别名)
  50. $alias = $params['alias'] ?? '';
  51. try {
  52. $push = self::getInstance()->push();
  53. //设置平台
  54. $push->setPlatform($platform);
  55. switch ($type) {
  56. case self::PUSH_TYPE_ALL:
  57. $push->addAllAudience();
  58. break;
  59. case self::PUSH_TYPE_TAG:
  60. $push->addTag($tag);
  61. break;
  62. case self::PUSH_TYPE_ALIAS:
  63. $push->addAlias($alias);
  64. break;
  65. case self::PUSH_TYPE_REG_ID:
  66. $push->addRegistrationId($reg_id);
  67. break;
  68. }
  69. $push->androidNotification($content, [
  70. 'title' => $title,
  71. 'builder_id' => $builder_id,
  72. 'extras' => $extras,
  73. ])->iosNotification($content, [
  74. 'sound' => 'sound',
  75. 'badge' => '+1',
  76. 'extras' => $extras
  77. ])->options([
  78. 'apns_production' => config('jpush.apns_production', true),
  79. //表示离线消息保留时长(秒)
  80. 'time_to_live' => 86400,
  81. ]);
  82. $response = $push->send();
  83. if ($response['http_code'] != 200) {
  84. Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE));
  85. }
  86. return $response;
  87. } catch (\Throwable $e) {
  88. Log::channel('jpush')->error(json_encode([
  89. 'file' => $e->getFile(),
  90. 'line' => $e->getLine(),
  91. 'message' => $e->getMessage(),
  92. 'params' => $params,
  93. ], JSON_UNESCAPED_UNICODE));
  94. }
  95. }
  96. /**
  97. * 获取指定设备的别名和标签
  98. */
  99. public static function getDevices($reg_id)
  100. {
  101. $response = self::getInstance()->device()->getDevices($reg_id);
  102. if ($response['http_code'] == 200) {
  103. return $response['body'];
  104. }
  105. return [];
  106. }
  107. /**
  108. * 给指定设备添加标签
  109. */
  110. public static function addTags($reg_id, $tags = [])
  111. {
  112. $response = self::getInstance()->device()->addTags($reg_id, $tags);
  113. if ($response['http_code'] == 200) {
  114. return true;
  115. }
  116. return false;
  117. }
  118. /**
  119. * 清空指定设备的标签
  120. */
  121. public static function clearTags($reg_id)
  122. {
  123. $response = self::getInstance()->device()->clearTags($reg_id);
  124. if ($response['http_code'] == 200) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /**
  130. * 清空指定设备的标签
  131. */
  132. public static function removeTags($reg_id, $tags = [])
  133. {
  134. $response = self::getInstance()->device()->removeTags($reg_id, $tags);
  135. if ($response['http_code'] == 200) {
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * 更新指定设备的别名
  142. */
  143. public static function updateAlias($reg_id, $alias)
  144. {
  145. $response = self::getInstance()->device()->updateAlias($reg_id, $alias);
  146. if ($response['http_code'] == 200) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. }