一、下載極光推播PHP SDK,解壓後放在/protected/components/目錄下,如下圖所示:
二、完善修改下官方的demo例子,我這裡複製一份demo,改為NotifyPush.php,如下程式碼:
<?php require dirname(__FILE__) . '/jpush-api-php-client/autoload.php'; use JPushClient 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 (JPushExceptionsAPIConnectionException $e) { // try something here print $e; } catch (JPushExceptionsAPIRequestException $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 (JPushExceptionsAPIConnectionException $e) { // try something here print $e; } catch (JPushExceptionsAPIRequestException $e) { // try something here print $e; } } }
三、呼叫NotifyPush.php裡的方法,實現推播,如下程式碼:
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); } }
相關教學:PHP視訊教學
以上就是Yii1.1框架模擬PHP極光推播訊息通知的詳細內容,更多請關注TW511.COM其它相關文章!