diff --git a/app/admin/controller/Notice.php b/app/admin/controller/Notice.php index fa13a565..60cab835 100644 --- a/app/admin/controller/Notice.php +++ b/app/admin/controller/Notice.php @@ -13,8 +13,8 @@ class Notice extends AdminBaseController return json($returnData); } - // 使用Pusher服务推送系统消息 - public function pushMessage() + // 向一个或多个设备兴趣推送通知 + public function publishToInterest() { $param = $this->request->post(); if (empty($param['title']) || empty($param['body'])) { @@ -25,10 +25,38 @@ class Notice extends AdminBaseController ]); } - $interestName = ["admin-popup-push"]; // 订阅兴趣的名称, 一次推送最多100个兴趣名称 - $title = ""; - $body = ""; - $res = (new \app\utility\Pusher())->publishToInterest($interestName, $title, $body); + $interestName = ["interests-1"]; // 订阅兴趣的名称, 一次推送最多100个兴趣名称 + $res = (new \app\utility\Pusher())->publishToInterest($interestName, $param['title'], $param['body']); + return json([ + 'code' => 0, + 'message' => 'ok', + 'data' => $res + ]); + } + + // 向一个或多个用户推送通知 + public function publishToUsers() + { + $param = $this->request->post(); + if (empty($param['users']) || empty($param['title']) || empty($param['body'])) { + return json([ + 'code' => 400, + 'message' => "缺少参数", + 'data' => [] + ]); + } + // 将用户ID构建为数组 + $explodeUser = explode(',', $param['users']); + $buildUserArr = []; + foreach ($explodeUser as $uid) { + if (is_numeric($uid)) { + $buildUserArr[] = strval($uid); + } else { + $buildUserArr[] = $uid; + } + } + // 发送通知 + $res = (new \app\utility\Pusher())->publishToUsers($buildUserArr, $param['title'], $param['body']); return json([ 'code' => 0, 'message' => 'ok', diff --git a/app/admin/route/app.php b/app/admin/route/app.php index 71b1e93b..b28cccfa 100644 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -34,7 +34,8 @@ Route::group('/', function () { // 消息推送 Route::post('notice/popup', 'Notice/popUp'); // 弹窗推送消息 - Route::post('notice/push_message', 'Notice/pushMessage'); // 使用Pusher推送设备兴趣通知 + Route::post('notice/publish_to_interest', 'Notice/publishToInterest'); // 使用Pusher向设备兴趣推送通知 + Route::post('notice/publish_to_users', 'Notice/publishToUsers'); // 使用Pusher向用户推送通知 Route::post('admin/send_email_or_sms', 'Admin/sendEmailOrSms'); // 给用户发邮件或者短信 Route::post('admin/batch_send_email_or_sms', 'Admin/batchSendEmailOrSms'); // 批量给用户发邮件或者短信 diff --git a/app/home/controller/Notice.php b/app/home/controller/Notice.php index 25a1cb9c..060e3356 100644 --- a/app/home/controller/Notice.php +++ b/app/home/controller/Notice.php @@ -1,9 +1,7 @@ middleware(\app\home\middleware\RepeatOperateMiddleware::class); diff --git a/app/utility/Pusher.php b/app/utility/Pusher.php index baa8d73c..40b29695 100644 --- a/app/utility/Pusher.php +++ b/app/utility/Pusher.php @@ -85,6 +85,9 @@ class Pusher extends BaseHomeService // 生成Beams身份验证令牌,该令牌有效期为24小时 (客户端需要拿这个Token去请求Beams关联设备与用户ID) 注意:每个平台每个用户在任意时刻最多可关联100台设备,当用户退出应用时,可以调用客户端SDK中的.stop方法理解解除关联关系。 public function generateToken($userId) { + if (is_numeric($userId)) { + $userId = strval($userId); + } $beamsClient = self::getClient(); return $beamsClient->generateToken($userId); }