YII2.0集成极光推送实现IOS的PUSH功能

极光组推和个推可以通过极光返回的tag标签、alias别名、registionID进行推送。

群推直接通过->setAudience(M\all)进行推送,推送保存一条记录到本地数据库即可。


附极光推送的PHP版本SDK下载网址:http://docs.jpush.io/server/php_sdk/

1、解压出来的verdor目录改名,如jpush放到YII的vendor目录。

2、index.php入口文件加载,如图:

192249_VX96_780509.png

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()方法即可。


转载于:https://my.oschina.net/chinahub/blog/478099

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值