Yii1.1框架模拟PHP极光推送消息通知-php教程

资源魔 33 0
本篇文章次要讲述的是用Yii框架模仿PHP极光推送音讯告诉,具备肯定的参考代价,感兴味的冤家能够理解一下,心愿对你有所协助。

1、下载极光推送PHP SDK,解压后放正在/protected/components/目次下,以下图所示:

2、欠缺修正下民间的demo例子,我这里复制一份demo,改成NotifyPush.php,以下代码:

<?php
require dirname(__FILE__) . '/jpush-api-php-client/autoload.php';
use JPush\Client as JPush;
class NotifyPush {
    static function pushAlias($alias,$ticket='音讯提示',$alert){
        $appKey=Yii::app()->params['push']['AppKey'];
        $appMasterSecret=Yii::app()->params['push']['AppMasterSecret'];
        $production_mode=Yii::app()->params['push']['production_mode'];



        $client = new JPush($appKey, $appMasterSecret);


// 完好的推送示例
// 这只是应用样例,不该该间接用于实际消费环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
                // 普通状况下,对于 audience 的设置只要要挪用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个办法中的某一个便可,这里仅作为示例,当然全副挪用也能够,多项 audience 挪用示意其后果的交加
                // 便是说普通状况下,上面三个办法以及不列出的 addTagAnd 一共四个,只实用一个即可餍足年夜少数的场景需要

                ->addAlias($alias)
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    'sound' => 'sound.caf',
                    // 'badge' => '+1',
                    // 'content-available' => true,
                    // 'mutable-content' => true,
                    'category' => 'jiguang',
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->androidNotification($ticket, array(
                    'title' => $alert,
                    // 'build_id' => 2,
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->message($alert, array(
                    'title' => $alert,
                    // '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' => $production_mode,

                    // big_push_duration: 示意定速推送时长(分钟),又名迟缓推送,把本来尽可能快的推送速率,升高上去,
                    // 给定的 n 分钟内,平均地向此次推送的指标用户推送。最年夜值为1400.未设置则没有是定速推送
                    // 这里设置为 1 仅作为示例

                    // 'big_push_duration' => 1
                ))
                ->send();
            print_r($response);

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }

    }

    static function pushAll($ticket='音讯提示',$alert){
        $appKey=Yii::app()->params['push']['AppKey'];
        $appMasterSecret=Yii::app()->params['push']['AppMasterSecret'];
        $production_mode=Yii::app()->params['push']['production_mode'];



        $client = new JPush($appKey, $appMasterSecret);


// 完好的推送示例
// 这只是应用样例,不该该间接用于实际消费环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
                ->setAudience('all')
                // 普通状况下,对于 audience 的设置只要要挪用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个办法中的某一个便可,这里仅作为示例,当然全副挪用也能够,多项 audience 挪用示意其后果的交加
                // 便是说普通状况下,上面三个办法以及不列出的 addTagAnd 一共四个,只实用一个即可餍足年夜少数的场景需要
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    'sound' => 'sound.caf',
                    // 'badge' => '+1',
                    // 'content-available' => true,
                    // 'mutable-content' => true,
                    'category' => 'jiguang',
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->androidNotification($ticket, array(
                    'title' => $alert,
                    // 'build_id' => 2,
                    'extras' => array(
                        'key' => 'value',
                        'jiguang'
                    ),
                ))
                ->message($alert, array(
                    'title' => $alert,
                    // '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' => $production_mode,

                    // big_push_duration: 示意定速推送时长(分钟),又名迟缓推送,把本来尽可能快的推送速率,升高上去,
                    // 给定的 n 分钟内,平均地向此次推送的指标用户推送。最年夜值为1400.未设置则没有是定速推送
                    // 这里设置为 1 仅作为示例

                    // 'big_push_duration' => 1
                ))
                ->send();
            print_r($response);

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // try something here
            print $e;
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // try something here
            print $e;
        }

    }
}

3、挪用NotifyPush.php里的办法,完成推送,以下代码:

class CronNotifyPushCo妹妹and extends  CConsoleCo妹妹and{
    public $keys=array();
    public function init(){
        parent::init();
    }

    public function actionIndex(){
        echo 'start CronNotifyPushCo妹妹and '.chr(10);
        if(!Yii::app()->params['push']['push_status']){
            echo 'push status disabled';die();
        }
        $rkey='message_notify_list';
        $waitTotals=Fredis::model()->redis->lsize($rkey);
        echo 'wait totals:'.$waitTotals.chr(10);
        $waitResult=true;
        $i=0;
        while($waitResult) {$i++;
            echo $i.'/'.$waitTotals.' wait to do'.chr(10);
            $waitResult=Fredis::model()->redis->rpop($rkey);
            if(!$waitResult) {
                continue;
            }
            $db_data=unserialize($waitResult);var_dump($db_data);
            $message_content=$db_data['message_content'];
            $uid=$db_data['uid'];
            $alias=$uid;
            if($uid==0){
                NotifyPush::pushAll($message_content, $message_content);
            }else {
                NotifyPush::pushAlias($alias, $message_content, $message_content);
            }
        }

        echo 'end'.chr(10);
    }
}

相干教程:PHP视频教程

以上就是Yii1.1框架模仿PHP极光推送音讯告诉的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 Yii1.1 极光推送

抱歉,评论功能暂时关闭!