Home >Backend Development >PHP Tutorial >Yii1.1 framework simulates PHP Aurora push message notification

Yii1.1 framework simulates PHP Aurora push message notification

little bottle
little bottleforward
2019-04-25 14:23:402452browse

This article mainly talks about using the Yii framework to simulate PHP Aurora push message notifications. It has certain reference value. Interested friends can learn about it. I hope it will be helpful to you.

1. Download the Aurora Push PHP SDK, unzip it and place it in the /protected/components/ directory, as shown below:

2. Complete the modifications Below is the official demo example. I copy a demo here and change it to NotifyPush.php, with the following code:

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



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


// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array(&#39;ios&#39;, &#39;android&#39;))
                // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
                // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求

                ->addAlias($alias)
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    &#39;sound&#39; => &#39;sound.caf&#39;,
                    // &#39;badge&#39; => &#39;+1&#39;,
                    // &#39;content-available&#39; => true,
                    // &#39;mutable-content&#39; => true,
                    &#39;category&#39; => &#39;jiguang&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->androidNotification($ticket, array(
                    &#39;title&#39; => $alert,
                    // &#39;build_id&#39; => 2,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->message($alert, array(
                    &#39;title&#39; => $alert,
                    // &#39;content_type&#39; => &#39;text&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->options(array(
                    // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                    // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                    // 这里设置为 100 仅作为示例

                    // &#39;sendno&#39; => 100,

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

                    // &#39;time_to_live&#39; => 1,

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

                    &#39;apns_production&#39; => $production_mode,

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

                    // &#39;big_push_duration&#39; => 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=&#39;消息提醒&#39;,$alert){
        $appKey=Yii::app()->params[&#39;push&#39;][&#39;AppKey&#39;];
        $appMasterSecret=Yii::app()->params[&#39;push&#39;][&#39;AppMasterSecret&#39;];
        $production_mode=Yii::app()->params[&#39;push&#39;][&#39;production_mode&#39;];



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


// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
        try {
            $response = $client->push()
                ->setPlatform(array(&#39;ios&#39;, &#39;android&#39;))
                ->setAudience(&#39;all&#39;)
                // 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd  或 addRegistrationId
                // 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
                // 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
                ->setNotificationAlert($alert)
                ->iosNotification($ticket, array(
                    &#39;sound&#39; => &#39;sound.caf&#39;,
                    // &#39;badge&#39; => &#39;+1&#39;,
                    // &#39;content-available&#39; => true,
                    // &#39;mutable-content&#39; => true,
                    &#39;category&#39; => &#39;jiguang&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->androidNotification($ticket, array(
                    &#39;title&#39; => $alert,
                    // &#39;build_id&#39; => 2,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->message($alert, array(
                    &#39;title&#39; => $alert,
                    // &#39;content_type&#39; => &#39;text&#39;,
                    &#39;extras&#39; => array(
                        &#39;key&#39; => &#39;value&#39;,
                        &#39;jiguang&#39;
                    ),
                ))
                ->options(array(
                    // sendno: 表示推送序号,纯粹用来作为 API 调用标识,
                    // API 返回时被原样返回,以方便 API 调用方匹配请求与返回
                    // 这里设置为 100 仅作为示例

                    // &#39;sendno&#39; => 100,

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

                    // &#39;time_to_live&#39; => 1,

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

                    &#39;apns_production&#39; => $production_mode,

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

                    // &#39;big_push_duration&#39; => 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;
        }

    }
}

Third, call the method in NotifyPush.php to implement push, as follows:

class CronNotifyPushCommand extends  CConsoleCommand{
    public $keys=array();
    public function init(){
        parent::init();
    }

    public function actionIndex(){
        echo 'start CronNotifyPushCommand '.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);
    }
}

Related tutorials: PHP video tutorial

The above is the detailed content of Yii1.1 framework simulates PHP Aurora push message notification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete