


Yii1.1 framework simulates PHP Aurora push message notification
Apr 25, 2019 pm 02:23 PMThis 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__) . '/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); // 完整的推送示例 // 這只是使用樣例,不應(yīng)該直接用于實際生產(chǎn)環(huán)境中 !! try { $response = $client->push() ->setPlatform(array('ios', 'android')) // 一般情況下,關(guān)于 audience 的設(shè)置只需要調(diào)用 addAlias、addTag、addTagAnd 或 addRegistrationId // 這四個方法中的某一個即可,這里僅作為示例,當(dāng)然全部調(diào)用也可以,多項 audience 調(diào)用表示其結(jié)果的交集 // 即是說一般情況下,下面三個方法和沒有列出的 addTagAnd 一共四個,只適用一個便可滿足大多數(shù)的場景需求 ->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 調(diào)用標(biāo)識, // API 返回時被原樣返回,以方便 API 調(diào)用方匹配請求與返回 // 這里設(shè)置為 100 僅作為示例 // 'sendno' => 100, // time_to_live: 表示離線消息保留時長(秒), // 推送當(dāng)前用戶不在線時,為該用戶保留多長時間的離線消息,以便其上線時再次推送。 // 默認(rèn) 86400 (1 天),最長 10 天。設(shè)置為 0 表示不保留離線消息,只有推送當(dāng)前在線的用戶可以收到 // 這里設(shè)置為 1 僅作為示例 // 'time_to_live' => 1, // apns_production: 表示APNs是否生產(chǎn)環(huán)境, // True 表示推送生產(chǎn)環(huán)境,F(xiàn)alse 表示要推送開發(fā)環(huán)境;如果不指定則默認(rèn)為推送生產(chǎn)環(huán)境 'apns_production' => $production_mode, // big_push_duration: 表示定速推送時長(分鐘),又名緩慢推送,把原本盡可能快的推送速度,降低下來, // 給定的 n 分鐘內(nèi),均勻地向這次推送的目標(biāo)用戶推送。最大值為1400.未設(shè)置則不是定速推送 // 這里設(shè)置為 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); // 完整的推送示例 // 這只是使用樣例,不應(yīng)該直接用于實際生產(chǎn)環(huán)境中 !! try { $response = $client->push() ->setPlatform(array('ios', 'android')) ->setAudience('all') // 一般情況下,關(guān)于 audience 的設(shè)置只需要調(diào)用 addAlias、addTag、addTagAnd 或 addRegistrationId // 這四個方法中的某一個即可,這里僅作為示例,當(dāng)然全部調(diào)用也可以,多項 audience 調(diào)用表示其結(jié)果的交集 // 即是說一般情況下,下面三個方法和沒有列出的 addTagAnd 一共四個,只適用一個便可滿足大多數(shù)的場景需求 ->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 調(diào)用標(biāo)識, // API 返回時被原樣返回,以方便 API 調(diào)用方匹配請求與返回 // 這里設(shè)置為 100 僅作為示例 // 'sendno' => 100, // time_to_live: 表示離線消息保留時長(秒), // 推送當(dāng)前用戶不在線時,為該用戶保留多長時間的離線消息,以便其上線時再次推送。 // 默認(rèn) 86400 (1 天),最長 10 天。設(shè)置為 0 表示不保留離線消息,只有推送當(dāng)前在線的用戶可以收到 // 這里設(shè)置為 1 僅作為示例 // 'time_to_live' => 1, // apns_production: 表示APNs是否生產(chǎn)環(huán)境, // True 表示推送生產(chǎn)環(huán)境,F(xiàn)alse 表示要推送開發(fā)環(huán)境;如果不指定則默認(rèn)為推送生產(chǎn)環(huán)境 'apns_production' => $production_mode, // big_push_duration: 表示定速推送時長(分鐘),又名緩慢推送,把原本盡可能快的推送速度,降低下來, // 給定的 n 分鐘內(nèi),均勻地向這次推送的目標(biāo)用戶推送。最大值為1400.未設(shè)置則不是定速推送 // 這里設(shè)置為 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; } } }
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez
