<?php
namespace App\Helper;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use Carbon\Carbon;
use App\Helper\Cache;
class OppoMessage
{
/**
* 推送URL
*/
const URL = 'https://api.push.oppomobile.com';
/**
* token过期时间
*/
const TOKEN_TTL = 43200;
/**
* 推送消息 单条
* @param $setting 消息配置
* @param $message 消息体
* @param $regId 用户regId
*/
public function send($setting, $message, $regId)
{
$url = sprintf('%s/server/v1/message/notification/unicast', self::URL);
$accessToken = $this->getAccessToken($setting);
$notification = [
'click_action_type' => 4,
'click_action_activity' => $message['url'],
'title' => $message['title'],
'content' => $message['content'],
'showStartTime' => null,
'showEndTime' => null,
'action_parameters' => $message['params'] ?? null,
];
$message = json_encode([
'target_type' => 2,
'target_value' => $regId,
'notification' => $notification
]);
$params = [
'message' => $message,
'auth_token' => $accessToken,
];
$this->request($url, $params);
}
/**
* 获取message_id
* @param setting 推送配置
* @param message 消息体
*/
public function getMessageId($setting, $message)
{
$url = sprintf('%s/server/v1/message/notification/save_message_content', self::URL);
$accessToken = $this->getAccessToken($setting);
$params = [
'click_action_type' => 4,
'click_action_activity' => $message['url'],
'title' => $message['title'],
'content' => $message['content'],
'action_parameters' => $message['params'] ?? null,
'auth_token' => $accessToken,
];
$result = $this->request($url, $params);
return $result['data']['message_id'];
}
/**
* 推送消息 多条
* @param setting 推送配置
* @param message 消息体
* @param regIds 推送用户regId以逗号隔开
*/
public function pushRegIds($setting, $message, $regIds)
{
$url = sprintf('%s/server/v1/message/notification/broadcast', self::URL);
$accessToken = $this->getAccessToken($setting);
$messageId = self::getMessageId($setting, $message);
$params = [
'message_id' => $messageId,
'target_type' => 2,
'target_value' => $regIds,
'auth_token' => $accessToken,
];
$this->request($url, $params);
}
/**
* 推送消息 广播
* @param setting 推送配置
* @param message 消息体
*/
public function pushAll($setting, $message)
{
$url = sprintf('%s/server/v1/message/notification/broadcast', self::URL);
$accessToken = $this->getAccessToken($setting);
$messageId = self::getMessageId($setting, $message);
$params = [
'message_id' => $messageId,
'target_type' => 2,
'auth_token' => $accessToken,
];
$this->request($url, $params);
}
/**
* 创建AccessToken
*/
public function createAccessToken($setting)
{
$timestamp = Carbon::now()->getPreciseTimestamp(3);
$sign = hash('sha256', $setting->app_key . $timestamp . $setting->master_secret);
$params = [
'app_key' => $setting->app_key,
'sign' => $sign,
'timestamp' => $timestamp,
];
$url = sprintf('%s/server/v1/auth', self::URL);
$result = $this->request($url, $params);
$key = $this->tokenKey($setting->app_key);
Cache::set($key, $result['data']['auth_token'], self::TOKEN_TTL);
return $result['data']['auth_token'];
}
/**
* 获取AccessToken
*/
public function getAccessToken($setting)
{
$key = $this->tokenKey($setting->app_key);
$accessToken = Cache::get($key);
if (!$accessToken) {
$accessToken = $this->CreateAccessToken($setting);
}
return $accessToken;
}
/**
* 获取缓存key
*/
public function tokenKey($appKey)
{
return sprintf('message:oppo:%s', $appKey);
}
/**
* 请求
*/
public function request($url, $params = [])
{
$options = [
'headers' => [
'Content-type' => 'application/x-www-form-urlencoded',
'charset' => 'UTF-8',
],
];
try {
$result = make(Client::class)
->request('FORM', $url, $params, $options)
->getResult();
if ($result['code'] !== 0) {
throw new BusinessException(ErrorCode::BIZ_ERROR, $result['message']);
}
} catch (\Exception $exception) {
throw new BusinessException(ErrorCode::BIZ_ERROR, $exception->getMessage());
}
return $result;
}
}
PHP 实现OPPO推送
于 2023-04-14 14:00:09 首次发布