PHP 实现OPPO推送

<?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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值