极光组推和个推可以通过极光返回的tag标签、alias别名、registionID进行推送。
群推直接通过->setAudience(M\all)进行推送,推送保存一条记录到本地数据库即可。
附极光推送的PHP版本SDK下载网址:http://docs.jpush.io/server/php_sdk/
1、解压出来的verdor目录改名,如jpush放到YII的vendor目录。
2、index.php入口文件加载,如图:
3、模型代码代码如下:
<?php
namespace tradingAPP\modelsV3;
use Yii;
use JPush\Model as M;
use JPush\JPushClient;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;
/**
* ios极光推送模型
* see https://www.jpush.cn/
* @author lg
*
*/
class Jpush extends \yii\db\ActiveRecord {
/**
* 使用极光的数据群推
* @param unknown $info
* @return boolean
*/
public static function pushAll($info) {
$app_key = Yii::$app->params ['Jpush'] ['app_key'];
$master_secret = Yii::$app->params ['Jpush'] ['master_secret'];
$client = new JPushClient ( $app_key, $master_secret );
$result = $client->push ()->setPlatform ( M\Platform ( 'ios' ) )->setAudience ( M\all )->setNotification ( M\notification ( M\ios ( "{$info['title']}", "happy", "+1", true, array (
"buyid" => $info ['buyid'],
"url" => $info ['url'],
"id" => $info['id']//供IOS定位到详细页
), "Ios8 Category" ) ) )->setOptions ( M\options ( null, 86400, null, true ) )->send ();
return $result;
}
public static function pushOne($registerationID,$username, $info) {
$model = new PushAuto ();
$model->attributes = array (
'PhoneType' => self::$phoneType,
'buyid' => $info ['buyid'],
'type' => $info ['type'],
'thumb' => $info ['thumb'],
'audience' => $info ['audience'],
'UUID' => $registerationID,
'username' => $username,
'title' => $info ['title'],
'message' => $info ['message'],
'totime' => $info ['totime'],
'url' => $info ['url']
);
$saveInfo = $model->saveInfo ();
$saveId = $saveInfo ['id'];
$info['id'] = $saveInfo ['id'];
$regIdArr = array ($registerationID);
$result = self::doPush ( $regIdArr, $info );
return $result;
}
/**
* 消息推送入口
* 注:ios 通知栏只显示标题,需要推内容,请在M\ios ( "{$title}",后添加即可
* 开发环境setOptions ( M\options ( null, 86400, null, false ) )
* 生产环境setOptions ( M\options ( null, 86400, null, true ) )
* @param array $regIdArr
* @param array $info
* @return boolean
*/
public static function doPush($regIdArr, $info) {
if (empty ( $regIdArr ) || empty ( $info )) {
return false;
}
$app_key = Yii::$app->params ['Jpush'] ['app_key'];
$master_secret = Yii::$app->params ['Jpush'] ['master_secret'];
$client = new JPushClient ( $app_key, $master_secret );
try {
$result = $client->push ()->setPlatform ( M\Platform ( 'ios' ) )->setAudience ( M\audience ( M\registration_id ( $regIdArr ) ) )->setNotification ( M\notification ( M\ios ( "{$info['title']}", "happy", "+1", true, array (
"buyid" => intval ( $info ['buyid'] ),
"url" => $info ['url'],
"id" => $info['id'],
), "Ios8 Category" ) ) )->setOptions ( M\options ( null, 86400, null, true ) )->send ();
return true;
} catch ( APIRequestException $e ) {
} catch ( APIConnectionException $e ) {
}
}
}
特别注释:
开发环境setOptions ( M\options ( null, 86400, null, false ) )下的推送
生产环境setOptions ( M\options ( null, 86400, null, true ) )下的推送
需要打印推送的效果,加参数->printJSON (),如:
控制器代码如下:
<?php
namespace tradingAPP\controllers;
use tradingAPP\modelsV3\Jpush;
class TestController extends \tradingAPP\components\Controller {
public function actionPushIos(){
$uuid = '0415835c25550';
$info = [
'title' => 'IOS推送测试'.mt_rand(1, 10000),
'message' => 'IOS推送测试IOS推送测试IOS推送测试IOS推送测试IOS推送测试',
'url' => 'http://m.ezhaojiaju.com/',
'thumb' => 'http://m.ezhaojiaju.com/images/sliderImg/slider-v3-1.png',
'type' => 1,
'UUID' => '27b0c8429bf509a3bb097fcac195b8ec308b6941f55f9a72ea3eec628ba15290',
'username' => 'JR_13442',
'audience' => 'single',
'buyid' => 0,
'PhoneType' => 2,
'totime' => ''
];
$res = Jpush::pushOne($uuid,'JR_13442', $info);
var_dump($res);
}
}
浏览器访问:
http://127.0.0.1/test/push-ios.html
即可以收到个推,群推访问模型pushAll()方法即可。