PHP极光推送整理

1 篇文章 0 订阅
1 篇文章 0 订阅

composer地址下载:composer require jpush/jpush;

github下载地址:https://github.com/jpush/jpush-api-php-client

官方文档:极光推送 - JPush 产品简介 - 极光文档

class JpushService
{
    protected $app_key = 'xxxxxxxxxxxx';//appkey
    protected $master_secret = 'xxxxxxxxxxx';// 密钥
    public $apns_production = false;//True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
    
    /**
     * 简单推送示例
     */
    public function simpleJpush($id)
    {
        $client = new JPush($this->app_key, $this->master_secret);
        $push_payload = $client->push()
            ->setPlatform(array('ios', 'android'))
            // ->setPlatform('all')
            ->addRegistrationId($id) //推送id
            // ->addAlias() // 别名
            // ->addAllAudience()
            // 默认推送--标准模式
            ->setNotificationAlert('Hi, JPush')
            // 单独设置IOS推送
            ->iosNotification("hi,ios jpush", array(
                "sound" => "iOS sound",
                "badge" => "+1",
                "category" => "JPush",
                "extras" => array()
            ))
            ->androidNotification("hi,android jpush", array(
                "title" => "title",
            ));
        try {
            $response = $push_payload->send();
            print_r($response);
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }
    }
    // 完整的推送示例
    // 这只是使用样例,不应该直接用于实际生产环境中 !!
    public function ss()
    {
        try {
            $client = new JPush($this->app_key, $this->master_secret);
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
                // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
                // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
                // ->addAlias('alias')
                // ->addTag(array('tag1', 'tag2'))
                // ->addRegistrationId($registration_id)
                ->addAllAudience()
                ->setNotificationAlert('Hi, JPush')
                ->iosNotification('Hello IOS', array(
                    'sound' => 'sound.caf',
                    // 'badge' => '+1',
                    // 'content-available' => true,
                    // 'mutable-content' => true,
                    'category' => 'jiguang',
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->androidNotification('Hello Android', array(
                    'title' => 'hello jpush',
                    // 'builder_id' => 2,
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->message('message content', array(
                    'title' => 'hello jpush',
                    // 'content_type' => 'text',
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->options(array(
                    // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                    // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                    // 这里设置为 100 仅作为示例

                    // 'sendno' => 100,

                    // time_to_live: 表示离线消息保留时长(秒),
                    // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
                    // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
                    // 这里设置为 1 仅作为示例

                    // 'time_to_live' => 1,

                    // apns_production: 表示APNs是否生产环境,
                    // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境

                    'apns_production' => false,

                    // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
                    // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
                    // 这里设置为 1 仅作为示例

                    // 'big_push_duration' => 1
                ))
                ->setSmsMessage(array(
                    'delay_time' => 60,
                    'signid' => 154,
                    'temp_id' => 1,
                    'temp_para' => array(
                        'code' => 357
                    ),
                    'active_filter' => false
                ))
                ->send();
            print_r($response);

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }
    }
 /**
     * 推送给所有用户
     * @param string $title 推送标题
     * @param string $description 推送内容
     * @param array $extras 扩展字段 接受一个数组,自定义 Key/value 信息以供业务使用
     * @return array
     */
    public function pushAllUsers($title = '', $description = '', $extras = array()): array
    {
        $client = new JPush($this->app_key, $this->master_secret);
        $push_payload = $client->push()
            ->setPlatform('all')
            ->addAllAudience()
            // 默认推送--标准模式
            ->setNotificationAlert("{$title}")
            // 单独设置IOS推送
            ->iosNotification("{$title}", array(
                "sound" => "iOS sound",
                "badge" => "+1",
                "category" => "JPush",
                "extras" => $extras
            ))
            // 单独设置Android推送
            ->androidNotification("{$description}", array(
                "title" => "{$title}",
                'extras' => $extras
            ))
            ->options(array(
                // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                // 这里设置为 100 仅作为示例
                # 'sendno' => 100,

                // time_to_live: 表示离线消息保留时长(秒),
                // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
                // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
                // 这里设置为 1 仅作为示例
                'time_to_live' => 86400,

                // apns_production: 表示APNs是否生产环境,
                // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
                'apns_production' => $this->apns_production,

                // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
                // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
                // 这里设置为 1 仅作为示例
                # 'big_push_duration' => 1
            ));
        try {
            $response = $push_payload->send();
            return array('code' => 200, 'msg' => 'ok', 'data' => $response);
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return array('code' => 500, 'msg' => 'ok', 'data' => $e);
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return array('code' => 500, 'msg' => 'ok', 'data' => $e);
        }
    }

 
/**
 * 单个用户推送
 * @param string $title 推送标题
 * @param string $description 推送内容
 * @param string registration_id  推送ID
 * @param array $extras 扩展字段 接受一个数组,自定义 Key/value 信息以供业务使用
 */
    public function pushSingleUsers($title, $description, $registration_id, $extras)
    {
        $client = new JPush($this->app_key, $this->master_secret);
        $push_payload = $client->push()
            ->setPlatform(array('ios', 'android'))
            ->addRegistrationId($registration_id)
            // 默认推送--标准模式
            ->setNotificationAlert("{$title}")
            // 单独设置IOS推送
            ->iosNotification("{$title}", array(
                "sound" => "iOS sound",
                "badge" => "+1",
                "category" => "JPush",
                "extras" => $extras
            ))
            // 单独设置Android推送
            ->androidNotification("{$description}", array(
                "title" => "{$title}",
                'extras' => $extras
            ))
            ->options(array(
                // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                // 这里设置为 100 仅作为示例
                # 'sendno' => 100,

                // time_to_live: 表示离线消息保留时长(秒),
                // 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
                // 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
                // 这里设置为 1 仅作为示例
                'time_to_live' => 86400,

                // apns_production: 表示APNs是否生产环境,
                // True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
                'apns_production' => $this->apns_production,

                // big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
                // 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
                // 这里设置为 1 仅作为示例
                # 'big_push_duration' => 1
            ));
        try {
            $response = $push_payload->send();
            return array('code' => 200, 'msg' => 'ok', 'data' => $response);
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return array('code' => 500, 'msg' => 'ok', 'data' => $e);
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return array('code' => 500, 'msg' => 'ok', 'data' => $e);
        }
    }

}

整理如上:如有错误或更好的方法,望指出~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值