php实现极光推送

直接上代码

<?php

use App\Services\JPushService;

/**
 * 极光推送
 * @param string $alias 别名  选填  最多一次推送一千个 限制字符长度40
 * @param string $title android推送标题  必填
 * @param string $alert ios推送标题  必填
 * @param string $content 推送内容  必填
 * @param string $extras 附加字段  选填
 * @param string $builderId 通知栏样式 ID
 * @param string $type 推送类型  必填  全部所有:1(每天可发送十次)    根据标签:2    根据别名:3   根据目标(注册ID):4
 * @param string $registrationId 注册ID  选填  最多一次推送一千个
 * @param string $tag 标签  选填  最多一次推送20个 限制字符长度40
 */
public Static function testJpush($alert='',$alias='',$title='',$content='',$extras='',$builderId='1',$type='',$registrationId='',$tag='')
{
    // 推送平台 ios android
    $params['platform'] = 'all';

    // android推送标题
    $params['title'] = $title;
    // ios推送标题
    $params['alert'] = $alert;

    // 推送内容
    $params['content'] = $content;

    // 通知栏样式 ID
    $params['builderId'] = $builderId;

    // 附加字段(这里自定义 Key / value 信息,以供业务使用)$params['extras'] = ['orderid' => 13545];
    $params['extras'] = $extras;

    // 别名 可以是单个 也可以是 数组$params['alias'] = ['51651545154','61654564897',];
    $params['alias'] = $alias;

    // 标签 可以是单个 也可以是 数组$params['tag'] = ['51651545154','61654564897',];
    $params['tag'] = $tag;

    // 注册ID 可以是单个 也可以是 数组$params['registrationId'] = ['170976fsdas554ewerr98f28','120c8545we15we46b8929e'];
    $params['registration_id'] = $registrationId;

    // 推送类型 全部所有:1    根据标签:2    根据别名:3   根据目标(注册ID):4
    $params['type'] = $type;

//        var_dump($params);
//        die;

    //执行
    $dat = JPushService::pushNotify($params);

    return $dat;
//        dump($dat);die;
}
/**
 * 获取指定设备的别名和标签
 */
public static function getDevices($reg_id)
{
    $response = JPushService::getInstance()->device()->getDevices($reg_id);
    if ($response['http_code'] == 200) {
        return $response['body'];
    }
    return [];
}
/**
 * 给指定设备添加标签
 */
public static function addTags($reg_id, $tags = [])
{
    $response = JPushService::getInstance()->device()->addTags($reg_id, $tags);
    if ($response['http_code'] == 200) {
        return true;
    }
    return false;
}
/**
 * 清空指定设备的标签
 */
public static function clearTags($reg_id)
{
    $response = JPushService::getInstance()->device()->clearTags($reg_id);
    if ($response['http_code'] == 200) {
        return true;
    }
    return false;
}
/**
 * 清空指定设备的标签
 */
public static function removeTags($reg_id, $tags = [])
{
    $response = JPushService::getInstance()->device()->removeTags($reg_id, $tags);
    if ($response['http_code'] == 200) {
        return true;
    }
    return false;
}
/**
 * 更新指定设备的别名
 */
public static function updateAlias($reg_id, $alias)
{
    $response = JPushService::getInstance()->device()->updateAlias($reg_id, $alias);
    if ($response['http_code'] == 200) {
        return true;
    }
    return false;
}
<?php
namespace App\Services;

use JPush\Client as JPush;
use Log;

class JPushService
{
    protected static $client = null;

    //推送类型
    //全部所有
    const PUSH_TYPE_ALL = 1;
    //根据标签
    const PUSH_TYPE_TAG = 2;
    //根据别名
    const PUSH_TYPE_ALIAS = 3;
    //根据目标(注册ID)
    const PUSH_TYPE_REG_ID = 4;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    /**
     * 获取实例
     */
    public static function getInstance()
    {
        if (!self::$client) {
            self::$client = new JPush(config('app_key'), config('master_secret'), null);
        }
        return self::$client;
    }

    /**
     * 给android或ios推送消息
     */
    public static function pushNotify($params)
    {
        //推送平台
        $platform = $params['platform'] ?? 'all';
        //android推送标题
        $title = $params['title'] ?? '';
        //iOS推送标题
        $alert = $params['alert'] ?? '';
        //推送内容
        $content = $params['content'] ?? '';
        //通知栏样式ID
        $builder_id = $params['builder_id'] ?? 0;
        //附加字段
        $extras = $params['extras'] ?? '';
        //推送类型
        $type = $params['type'] ?? '';

        //推送目标(注册ID)
        $reg_id = $params['registration_id'] ?? '';
        //推送目标(标签)
        $tag = $params['tag'] ?? '';
        //推送目标(别名)
        $alias = $params['alias'] ?? '';

        try {
            $push = self::getInstance()->push();

            //设置平台
            $push->setPlatform($platform);

            switch ($type) {
                case self::PUSH_TYPE_ALL:
                    $push->addAllAudience();
                    break;
                case self::PUSH_TYPE_TAG:
                    $push->addTag($tag);
                    break;
                case self::PUSH_TYPE_ALIAS:
                    $push->addAlias($alias);
                    break;
                case self::PUSH_TYPE_REG_ID:
                    $push->addRegistrationId($reg_id);
                    break;
            }

            $push->androidNotification($content, [
                'title' => $title,
                'builder_id' => $builder_id,
                'extras' => $extras,
            ])->iosNotification(array(
                'title' => $alert, //可选设置
//                'subtitle' => 'subtitle', //可选设置
                'body' => $content//必填,否则通知栏不展示
            ), [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => $extras
            ])->options([
                'apns_production' => config('jpush.apns_production', true),
                //表示离线消息保留时长(秒)
                'time_to_live' => 86400,
            ]);

            $response = $push->send();

            if ($response['http_code'] != 200) {
                Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE));
            }

            return $response;
        } catch (\Throwable $e) {
            Log::channel('jpush')->error(json_encode([
                'file' => $e->getFile(),
                'line' => $e->getLine(),
                'message' => $e->getMessage(),
                'params' => $params,
            ], JSON_UNESCAPED_UNICODE));
        }
    }

    /**
     * 获取指定设备的别名和标签
     */
    public static function getDevices($reg_id)
    {
        $response = self::getInstance()->device()->getDevices($reg_id);

        if ($response['http_code'] == 200) {
            return $response['body'];
        }

        return [];
    }

    /**
     * 给指定设备添加标签
     */
    public static function addTags($reg_id, $tags = [])
    {
        $response = self::getInstance()->device()->addTags($reg_id, $tags);

        if ($response['http_code'] == 200) {
            return true;
        }

        return false;
    }

    /**
     * 清空指定设备的标签
     */
    public static function clearTags($reg_id)
    {
        $response = self::getInstance()->device()->clearTags($reg_id);

        if ($response['http_code'] == 200) {
            return true;
        }

        return false;
    }

    /**
     * 清空指定设备的标签
     */
    public static function removeTags($reg_id, $tags = [])
    {
        $response = self::getInstance()->device()->removeTags($reg_id, $tags);

        if ($response['http_code'] == 200) {
            return true;
        }

        return false;
    }

    /**
     * 更新指定设备的别名
     */
    public static function updateAlias($reg_id, $alias)
    {
        $response = self::getInstance()->device()->updateAlias($reg_id, $alias);

        if ($response['http_code'] == 200) {
            return true;
        }

        return false;
    }
}

版权声明:本文为weixin_44900765原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。