123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Modules\Push\Services;
- use App\Consts\ErrorConst;
- use App\Consts\PushConst;
- use App\Libs\Push\HuaWei\HwPushCommon;
- use App\Libs\Push\OPPOPush\OPPOPushCommon;
- use App\Libs\Push\XMPush\MiPushCommon;
- use App\Libs\Utils;
- use App\Modules\Push\Models\QappPushApp;
- use App\Modules\Push\Models\QappPushUser;
- use Exception;
- use GuzzleHttp\Exception\GuzzleException;
- use App\Exceptions\ApiException;
- class PushMessageService
- {
- /**
- * @param $uid
- * @param $title
- * @param $content
- * @param $url
- * @return array|bool|mixed|string|null
- * @throws ApiException
- * @throws GuzzleException
- */
- public static function pushMessageToUser($uid, $title, $content, $url)
- {
- // 获取用户push信息
- $pushUser = QappPushUser::getPushUserByUid($uid);
- $regId = getProp($pushUser, 'reg_id');
- $regIdList = [$regId];
- if (empty($regId)) {
- Utils::throwError(ErrorConst::PUSH_TOKEN_INVALID);
- }
- // 获取push app信息
- $pushAppId = getProp($pushUser, 'app_id');
- $pushApp = QappPushApp::getPushAppByAppId($pushAppId);
- $package = getProp($pushApp, 'package');
- $appId = getProp($pushApp, 'app_id');
- $provider = getProp($pushApp, 'provider');
- $appSecret = getProp($pushApp, 'app_secret');
- $appKey = getProp($pushApp, 'app_key');
- $masterSecret = getProp($pushApp, 'master_secret');
- $result = [];
- try {
- switch ($provider) {
- // 华为
- case PushConst::PROVIDER_HW:
- // 初始化huawei推送
- $client = new HwPushCommon($appId, $appSecret);
- // 循环推送
- $client->setToken($regIdList);
- $result = $client->sendPushMessage($title, $content, $url);
- break;
- // 小米
- case PushConst::PROVIDER_MI:
- // 初始化小米推送
- $client = new MiPushCommon($package, $appSecret);
- // 循环推送
- $client->setRegArr($regIdList);
- $result = $client->sendMessage($title, $content, $url);
- break;
- // OPPO
- case PushConst::PROVIDER_OPPO:
- // 初始化oppo推送
- $client = new OPPOPushCommon($appKey, $masterSecret);
- $messageId = $client->getMessageId($title, $content, $url);
- // 循环推送
- $client->setRegArr($regIdList);
- $result = $client->broadCastRegIds($messageId);
- break;
- }
- } catch (Exception $e) {
- $message = $e->getMessage();
- myLog('push')->info('Exception', compact('result', 'message'));
- Utils::throwError(ErrorConst::PUSH_FAIELD);
- }
- return $result;
- }
- }
|