Push.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * \~chinese
  26. * \brief
  27. * 设置推送昵称
  28. *
  29. * \details
  30. * 设置用户的推送昵称,在离线推送时使用。
  31. *
  32. * @param string $username 用户名
  33. * @param string $nickname 要设置的推送昵称
  34. * @return boolean|array 成功或者错误
  35. *
  36. * \~english
  37. * \brief
  38. * Set push nickname
  39. *
  40. * \details
  41. * Set the user's push nickname and use it when pushing offline.
  42. *
  43. * @param string $username User name
  44. * @param string $nickname Nickname
  45. * @return boolean|array Success or error
  46. */
  47. public function updateUserNickname($username, $nickname)
  48. {
  49. if (!trim($username) || !trim($nickname)) {
  50. \Easemob\exception('Please enter your username and nickname');
  51. }
  52. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  53. $body = compact('nickname');
  54. $resp = Http::put($uri, $body, $this->auth->headers());
  55. if (!$resp->ok()) {
  56. return \Easemob\error($resp);
  57. }
  58. return true;
  59. }
  60. /**
  61. * \~chinese
  62. * \brief
  63. * 设置推送消息展示方式
  64. *
  65. * \details
  66. * 设置推送消息至客户端的方式,修改后及时有效。服务端对应不同的设置,向用户发送不同展示方式的消息。
  67. *
  68. * @param string $username 用户名
  69. * @param int $notification_display_style 消息提醒方式,0:仅通知;1:通知以及消息详情;
  70. * @return boolean|array 成功或者错误
  71. *
  72. * \~english
  73. * \brief
  74. * Set push message display method
  75. *
  76. * \details
  77. * 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.
  78. *
  79. * @param string $username User name
  80. * @param int $notification_display_style Message reminder method, 0: notification only; 1: Notice and message details;
  81. * @return boolean|array Success or error
  82. */
  83. public function setNotificationDisplayStyle($username, $notification_display_style = 1)
  84. {
  85. if (!trim($username)) {
  86. \Easemob\exception('Please enter your username');
  87. }
  88. $notification_display_style = (int)$notification_display_style ? 1 : 0;
  89. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  90. $body = compact('notification_display_style');
  91. $resp = Http::put($uri, $body, $this->auth->headers());
  92. if (!$resp->ok()) {
  93. return \Easemob\error($resp);
  94. }
  95. return true;
  96. }
  97. /**
  98. * \~chinese
  99. * \brief
  100. * 设置推送免打扰
  101. *
  102. * \details
  103. * 设置用户免打扰,在免打扰期间,用户将不会收到离线消息推送。
  104. *
  105. * @param string $username 用户名
  106. * @param int $startTime 免打扰起始时间,单位是小时,例如 8 代表每日 8:00 开启免打扰
  107. * @param int $endTime 免打扰结束时间,单位是小时,例如 18 代表每日 18:00 关闭免打扰
  108. * @return boolean|array 成功或者错误
  109. *
  110. * \~english
  111. * \brief
  112. * Set no disturb
  113. *
  114. * \details
  115. * Set the user to be undisturbed. During the undisturbed period, the user will not receive offline message push.
  116. *
  117. * @param string $username User name
  118. * @param int $startTime The starting time of no disturbance, in hours, for example, 8 represents 8:00 every day
  119. * @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
  120. * @return boolean|array Success or error
  121. */
  122. public function openNotificationNoDisturbing($username, $startTime, $endTime)
  123. {
  124. return $this->disturb($username, 1, $startTime, $endTime);
  125. }
  126. /**
  127. * \~chinese
  128. * \brief
  129. * 取消推送免打扰
  130. *
  131. * @param string $username 用户名
  132. * @return boolean|array 成功或者错误
  133. *
  134. * \~english
  135. * \brief
  136. * Cancel push without interruption
  137. *
  138. * @param string $username User name
  139. * @return boolean|array Success or error
  140. */
  141. public function closeNotificationNoDisturbing($username)
  142. {
  143. return $this->disturb($username, 0);
  144. }
  145. /**
  146. * @ignore 设置免打扰
  147. * @param string $username 用户名
  148. * @param int $notification_no_disturbing 是否免打扰,0:代表免打扰关闭,1:免打扰开启
  149. * @param int $notification_no_disturbing_start 免打扰起始时间,单位是小时,例如 8 代表每日 8:00 开启免打扰
  150. * @param int $notification_no_disturbing_end 免打扰结束时间,单位是小时,例如 18 代表每日 18:00 关闭免打扰
  151. * @return boolean|array 成功或者错误
  152. */
  153. private function disturb(
  154. $username,
  155. $notification_no_disturbing,
  156. $notification_no_disturbing_start = 0,
  157. $notification_no_disturbing_end = 0
  158. ) {
  159. if (!trim($username)) {
  160. \Easemob\exception('Please enter your username');
  161. }
  162. $notification_no_disturbing = (int)$notification_no_disturbing ? 1 : 0;
  163. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  164. $body = compact('notification_no_disturbing', 'notification_no_disturbing_start', 'notification_no_disturbing_end');
  165. $resp = Http::put($uri, $body, $this->auth->headers());
  166. if (!$resp->ok()) {
  167. return \Easemob\error($resp);
  168. }
  169. return true;
  170. }
  171. }