1. 程式人生 > >tp5.0使用極光推送

tp5.0使用極光推送

第一步:phpstrom裡開啟專案,開啟cmd執行命令composer require jpush/jpush 安裝

第二步:表單頁面:

<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="" accept-charset="UTF-8">
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-title" class="form-control form-control" name="row[title]" type="text">
        </div>
    </div>
    <div class="form-group" id="push_content" >
        <label class="control-label col-xs-12 col-sm-2">{:__('Push_content')}:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea id="c-push_content" class="form-control " name="row[push_content]" data-rule="required"></textarea>
        </div>
    </div>

    <div class="form-group" id="platform">
        <label class="control-label col-xs-12 col-sm-2">{:__('Platform')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_radios('row[platform]',['全部','IOS','Android'])}
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">推送應用:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_checkboxs('row[app]',$appList,$select)}
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Target_population')}:</label>
        <div class="col-xs-12 col-sm-8" id="range">
            {:build_radios('row[target_population]',['全部使用者','指定使用者'])}
        </div>
    </div>

    <div class="form-group" id="appoint_people" style="display: none;">
        <label class="control-label col-xs-12 col-sm-2">{:__('Appoint_people')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-appoint_people" name="row[appoint_people]" data-source="platform/Jpushlog/selectpage" data-pagination="true" data-page-size="10" data-multiple="true" class="form-control selectpage form-control" >
        </div>
    </div>

    <!--<div class="form-group">-->
        <!--<label class="control-label col-xs-12 col-sm-2">{:__('Follow_upaction')}:</label>-->
        <!--<div class="col-xs-12 col-sm-8">-->
            <!--{:build_radios('row[follow_upaction]',['開啟應用','應用內開啟連結','開啟指定頁面','開啟書籍','應用外開啟連結'])}-->
        <!--</div>-->
    <!--</div>-->
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_radios('row[type]', ['開啟應用','商品詳情','webview','safari','聯絡導師'])}
        </div>
    </div>

    <div class="form-group" id="url" style="display: none">
        <label class="control-label col-xs-12 col-sm-2">跳轉連結:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-url" class="form-control" name="row[url]" >
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Uniform_speed')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_radios('row[uniform_speed]',['否','是'])}
        </div>
    </div>

    <div class="form-group" id="minute" style="display: none">
        <label class="control-label col-xs-12 col-sm-2">勻速推送時間(分鐘):</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-minute" class="form-control" name="row[minute]" data-rule="integer" >
        </div>
    </div>

    <div class="form-group" id="push_time">
        <label class="control-label col-xs-12 col-sm-2">{:__('Push_time')}:</label>
        <div class="col-xs-12 col-sm-8">
            {:build_radios('row[push_time]',['立即','定時'])}
        </div>
    </div>

    <div class="form-group" id="fixed_time" style="display: none;">
        <label class="control-label col-xs-12 col-sm-2">{:__('Fixed_time')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-fixed_time" class="form-control datetimepicker"  name="row[fixed_time]" >
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">{:__('Desc')}:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-desc" class="form-control form-control" name="row[desc]" type="text">
        </div>
    </div>
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <button type="submit" class="btn btn-success btn-embossed">{:__('OK')}</button>
            <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
        </div>
    </div>
</form>

第三步:控制器裡:

use JPush\Client;
use JPush\Exceptions\APIConnectionException;
use JPush\Exceptions\APIRequestException;
public function push($data)
{
    $platform = ['all', 'ios', 'android'];
    $app      = explode(',', $data['app']);
    $platform = $platform[$data['platform']];
    $audience = $data['target_population'] ? explode(',', $data['appoint_people']) : 'all';
    $alert    = $data['push_content'];
    $ios_notification = [
        'title'  => $data['title'],
        'badge'  => '+1',
    ];
    $android_notification = [
        'title'  => $data['title'],
    ];
    if($data['type']){
        $extras   = [
            'type'   => $data['type'],
            'data'   => ['param' => $data['url']]
        ];
        $ios_notification['extras']     = $extras;
        $android_notification['extras'] = $extras;
    }
    $uniform_speed = $data['uniform_speed'];
    $push_time     = $data['push_time'];
    $options       = ['apns_production'   => true];
    foreach ($app as $v) {
        $app    = model('app')->where('id', $v)->field('key,secret,platform')->find();
        $client = new Client($app['key'], $app['secret']);
        try{
            $push   = $client->push();
            $cid    = $push->getCid(1,'push');
            $res    = $push->setCid($cid['body']['cidlist'][0])->setPlatform($platform);
            if ($audience == 'all') {
                $res->addAllAudience();
            } else {
                $res->addAlias($audience);
            }
            $res->iosNotification($alert, $ios_notification)->androidNotification($alert, $android_notification);
            if ($uniform_speed) {
                $options['big_push_duration'] = (int)$data['minute'];
            }
            $res->options($options);
            if($push_time){
                $trigger  = ['time' => $data['fixed_time']];
                $schedule = $client->schedule();
                $schedule->createSingleSchedule('定時推送', $res->build(), $trigger);
            }else{
                $res->send();
            }
        }catch (APIConnectionException $e) { } catch (APIRequestException $e) { }
    }
    if ($push_time) {
        $data['fixed_time'] = strtotime($data['fixed_time']);
    } else {
        $data['is_push']    = 1;
        $data['fixed_time'] = 0;
    }
    return $this->model->allowField(true)->save($data);
}