Push.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Push 用来管理用户推送(设置推送免打扰等)
  7. *
  8. * \~english
  9. * The `Push` is used to manage user push (set push free, etc.)
  10. */
  11. final class Push
  12. {
  13. /**
  14. * @ignore
  15. * @var Auth $auth 授权对象
  16. */
  17. private $auth;
  18. /// @cond
  19. public function __construct($auth)
  20. {
  21. $this->auth = $auth;
  22. }
  23. /// @endcond
  24. //以同步方式发送推送通知
  25. //https://docs-im-beta.easemob.com/push/push_send_notification.html
  26. public function push_sync($uid, $pushMessage)
  27. {
  28. $uri = $this->auth->getBaseUri() . '/push/sync/' . $uid;
  29. $strategy = 0;
  30. $body = compact('strategy','pushMessage');
  31. $header = $this->auth->headers();
  32. $header['Content-Type'] = 'application/json';
  33. $resp = Http::post($uri, $body, $header);
  34. if (!$resp->ok()) {
  35. return \Easemob\error($resp);
  36. }
  37. $data = $resp->data();
  38. return $data['data'];
  39. //return $data['data']['successKeys'];
  40. }
  41. //创建全量推送任务
  42. //https://docs-im-beta.easemob.com/push/push_send_notification.html
  43. public function push_task($pushMessage)
  44. {
  45. $uri = $this->auth->getBaseUri() . '/push/task';
  46. $strategy = 0;
  47. $body = compact('strategy','pushMessage');
  48. $header = $this->auth->headers();
  49. $header['Content-Type'] = 'application/json';
  50. $resp = Http::post($uri, $body, $header);
  51. if (!$resp->ok()) {
  52. return \Easemob\error($resp);
  53. }
  54. $data = $resp->data();
  55. return $data['data'];
  56. //return $data['data']['successKeys'];
  57. }
  58. /**
  59. * \~chinese
  60. * \brief
  61. * 设置推送昵称
  62. *
  63. * \details
  64. * 设置用户的推送昵称,在离线推送时使用。
  65. *
  66. * @param string $username 用户名
  67. * @param string $nickname 要设置的推送昵称
  68. * @return boolean|array 成功或者错误
  69. *
  70. * \~english
  71. * \brief
  72. * Set push nickname
  73. *
  74. * \details
  75. * Set the user's push nickname and use it when pushing offline.
  76. *
  77. * @param string $username User name
  78. * @param string $nickname Nickname
  79. * @return boolean|array Success or error
  80. */
  81. public function updateUserNickname($username, $nickname)
  82. {
  83. if (!trim($username) || !trim($nickname)) {
  84. \Easemob\exception('Please enter your username and nickname');
  85. }
  86. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  87. $body = compact('nickname');
  88. $resp = Http::put($uri, $body, $this->auth->headers());
  89. if (!$resp->ok()) {
  90. return \Easemob\error($resp);
  91. }
  92. return true;
  93. }
  94. /**
  95. * \~chinese
  96. * \brief
  97. * 设置推送消息展示方式
  98. *
  99. * \details
  100. * 设置推送消息至客户端的方式,修改后及时有效。服务端对应不同的设置,向用户发送不同展示方式的消息。
  101. *
  102. * @param string $username 用户名
  103. * @param int $notification_display_style 消息提醒方式,0:仅通知;1:通知以及消息详情;
  104. * @return boolean|array 成功或者错误
  105. *
  106. * \~english
  107. * \brief
  108. * Set push message display method
  109. *
  110. * \details
  111. * Set the method of pushing messages to the client, which is timely and effective after modification. The server sends messages with different display methods to users according to different settings.
  112. *
  113. * @param string $username User name
  114. * @param int $notification_display_style Message reminder method, 0: notification only; 1: Notice and message details;
  115. * @return boolean|array Success or error
  116. */
  117. public function setNotificationDisplayStyle($username, $notification_display_style = 1)
  118. {
  119. if (!trim($username)) {
  120. \Easemob\exception('Please enter your username');
  121. }
  122. $notification_display_style = (int)$notification_display_style ? 1 : 0;
  123. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  124. $body = compact('notification_display_style');
  125. $resp = Http::put($uri, $body, $this->auth->headers());
  126. if (!$resp->ok()) {
  127. return \Easemob\error($resp);
  128. }
  129. return true;
  130. }
  131. /**
  132. * \~chinese
  133. * \brief
  134. * 设置推送免打扰
  135. *
  136. * \details
  137. * 设置用户免打扰,在免打扰期间,用户将不会收到离线消息推送。
  138. *
  139. * @param string $username 用户名
  140. * @param int $startTime 免打扰起始时间,单位是小时,例如 8 代表每日 8:00 开启免打扰
  141. * @param int $endTime 免打扰结束时间,单位是小时,例如 18 代表每日 18:00 关闭免打扰
  142. * @return boolean|array 成功或者错误
  143. *
  144. * \~english
  145. * \brief
  146. * Set no disturb
  147. *
  148. * \details
  149. * Set the user to be undisturbed. During the undisturbed period, the user will not receive offline message push.
  150. *
  151. * @param string $username User name
  152. * @param int $startTime The starting time of no disturbance, in hours, for example, 8 represents 8:00 every day
  153. * @param int $endTime The end time of no disturbance, in hours, for example, 18 means that no disturbance is closed at 18:00 every day
  154. * @return boolean|array Success or error
  155. */
  156. public function openNotificationNoDisturbing($username, $startTime, $endTime)
  157. {
  158. return $this->disturb($username, 1, $startTime, $endTime);
  159. }
  160. /**
  161. * \~chinese
  162. * \brief
  163. * 取消推送免打扰
  164. *
  165. * @param string $username 用户名
  166. * @return boolean|array 成功或者错误
  167. *
  168. * \~english
  169. * \brief
  170. * Cancel push without interruption
  171. *
  172. * @param string $username User name
  173. * @return boolean|array Success or error
  174. */
  175. public function closeNotificationNoDisturbing($username)
  176. {
  177. return $this->disturb($username, 0);
  178. }
  179. /**
  180. * @ignore 设置免打扰
  181. * @param string $username 用户名
  182. * @param int $notification_no_disturbing 是否免打扰,0:代表免打扰关闭,1:免打扰开启
  183. * @param int $notification_no_disturbing_start 免打扰起始时间,单位是小时,例如 8 代表每日 8:00 开启免打扰
  184. * @param int $notification_no_disturbing_end 免打扰结束时间,单位是小时,例如 18 代表每日 18:00 关闭免打扰
  185. * @return boolean|array 成功或者错误
  186. */
  187. private function disturb(
  188. $username,
  189. $notification_no_disturbing,
  190. $notification_no_disturbing_start = 0,
  191. $notification_no_disturbing_end = 0
  192. ) {
  193. if (!trim($username)) {
  194. \Easemob\exception('Please enter your username');
  195. }
  196. $notification_no_disturbing = (int)$notification_no_disturbing ? 1 : 0;
  197. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  198. $body = compact('notification_no_disturbing', 'notification_no_disturbing_start', 'notification_no_disturbing_end');
  199. $resp = Http::put($uri, $body, $this->auth->headers());
  200. if (!$resp->ok()) {
  201. return \Easemob\error($resp);
  202. }
  203. return true;
  204. }
  205. }