Yii 1.1 프레임 워 크 는 PHP 극광 푸 시 메시지 알림 기능 을 실현 합 니 다.

1.극광 푸 시 PHP SDK 를 다운로드 하여 압축 을 풀 고/protected/coponents/디 렉 터 리 에 놓 습 니 다.아래 그림 과 같 습 니 다.

2.공식 demo 예 를 보완 하고 수정 합 니 다.저 는 demo 를 복사 하여 Notify Push.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.Notify Push.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);
 }
}
총결산
위 에서 말씀 드 린 것 은 편집장 님 께 서 소개 해 주신 Yii 1.1 프레임 워 크 입 니 다.PHP 극광 푸 시 메시지 알림 을 실현 하고 여러분 께 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기