Push.php 6.8 KB

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