Thinkphp5.0整合个推开发APP实现推送流程

2019-03-2717:40:29后端程序开发Comments3,052 views字数 5039阅读模式

做一个后台发送消息推送到app(android和ios)的功能,该功能采用的是个推接口,基于php的,我用TP5来实现这个推送流程。先看官方demo吧。可以先参考官方给到的例子来看。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

由于该APP下载量比较大,考虑到要推送给所有下载app的用户,所以这里就不能用针对于单个用户进行推送了,必须针对应用群体进行推送。场景如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

Thinkphp5.0整合个推开发APP实现推送流程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

文档中给到的php例子是这个:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

<?php
//消息推送Demoheader("Content-Type: text/html; charset=utf-8");
require_once(dirname(__FILE__) . '/' . '');
define('APPKEY','请输入您的APPKEY');
define('APPID','请输入您的APPID');
define('MASTERSECRET','请输入您的MASTERSECRET');
define('HOST','');
define('CID','请输入您的CID');
pushMessageToApp();
function pushMessageToApp(){
    $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
    $template = IGtLinkTemplateDemo();
    //个推信息体
    //基于应用消息体$message = new IGtAppMessage();
    $message->set_isOffline(true);
    $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2$message->set_data($template);

    $appIdList=array(APPID);
    $phoneTypeList=array('ANDROID');
    $provinceList=array('浙江');
    $tagList=array('haha');

    $cdt = new AppConditions();
    $cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
    $cdt->addCondition(AppConditions::REGION, $provinceList);
    $cdt->addCondition(AppConditions::TAG, $tagList);

    $message->set_appIdList($appIdList);
    $message->set_conditions($cdt);

    $rep = $igt->pushMessageToApp($message);

    var_dump($rep);
    echo ("<br><br>");
}

function IGtLinkTemplateDemo(){
    $template =  new IGtLinkTemplate();
    $template ->set_appId(APPID);//应用appid$template ->set_appkey(APPKEY);//应用appkey$template ->set_title("请输入通知标题");//通知栏标题$template ->set_text("请输入通知内容");//通知栏内容$template ->set_logo("");//通知栏logo$template ->set_isRing(true);//是否响铃$template ->set_isVibrate(true);//是否震动$template ->set_isClearable(true);//通知栏是否可清除$template ->set_url("");//打开连接地址
    //$template->set_notifyStyle(0);
    //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
    //iOS推送需要设置的pushInfo字段
//        $apn = new IGtAPNPayload();
//        $apn->alertMsg = "alertMsg";
//        $apn->badge = 11;
//        $apn->actionLocKey = "启动";
//    //        $apn->category = "ACTIONABLE";
//    //        $apn->contentAvailable = 1;
//        $apn->locKey = "请输入通知栏内容";
//        $apn->title = "请输入通知栏标题";
//        $apn->titleLocArgs = array("titleLocArgs");
//        $apn->titleLocKey = "请输入通知栏标题";
//        $apn->body = "body";
//        $apn->customMsg = array("payload"=>"payload");
//        $apn->launchImage = "launchImage";
//        $apn->locArgs = array("locArgs");
//
//        $apn->sound=("");;
//        $template->set_apnInfo($apn);return$template;
}
?>

然后下载sdk,下载地址:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

然后看我自己实现流程:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

当我在后台对某一条消息进行推送的时候 利用ajax传过来消息标题和内容,在控制器中用一个方法去接收:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

//手机推送publicfunction pushPhone()
    {
        $this->base();
        if ($this->token == 2) {
            return '';
        }
        $list_id = Request::instance()->param('list_id');
        $list_title = Request::instance()->param('list_title');
        $info = new geTui();//实例化个推类
        $listId = [
            'type' => 'list',
            'value' => $list_id,
            'title' => '消息标题',
            'content' => $list_title,//消息内容
        ];
        $res = $info->pushMessageToApp($listId);/调用推送类
       
        if ($res['result'] == 'ok') {
            return ['code' => '1', 'msg' => '推送成功!'];
        } else {
            return ['code' => '0', 'msg' => '推送失败!'];
        }
    }

看一下这个geTui类存放位置以及代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

我把下载的sdk放在extend文件夹下了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

Thinkphp5.0整合个推开发APP实现推送流程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

然后重新创建了一个名为geTui的php文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

<?php
namespace app\push;
use think\Loader;

class GeTui
{
    private$host = '';

    //测试private$appkey = '';
    private$appid = '';
    private$mastersecret = '';

    //群推接口案例function pushMessageToApp($mes,$listId){
        import('', '', '.');
        $igt = new \IGeTui($this->host, $this->appkey, $this->mastersecret);
        $template = $this->IGtTransmissionTemplateDemos($mes,$listId);
        //$template = IGtLinkTemplateDemo();
        //个推信息体
        //基于应用消息体
        Loader::import('getui\igetui\', EXTEND_PATH);
        $message = new \IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2$message->set_data($template);

        $appIdList=array($this -> appid);
        $phoneTypeList=array('ANDROID');//忽略了
        $provinceList=array('浙江');//这个也忽略了
        $tagList=array('haha');
        //用户属性
        //$age = array("0000", "0010");


        //$cdt = new AppConditions();
        // $cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
        // $cdt->addCondition(AppConditions::REGION, $provinceList);
        //$cdt->addCondition(AppConditions::TAG, $tagList);
        //$cdt->addCondition("age", $age);$message->set_appIdList($appIdList);
        //$message->set_conditions($cdt->getCondition());$rep = $igt->pushMessageToApp($message);

        return$rep;
    }

//所有推送接口均支持四个消息模板,依次为通知弹框下载模板,通知链接模板,通知透传模板,透传模板
//注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能function IGtTransmissionTemplateDemos($mes,$listId){
        import('', '', '.');//引入sdk文件
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId($this -> appid);//应用appid$template->set_appkey($this->appkey);//应用appkey$template->set_transmissionType(2);//透传消息类型$template->set_transmissionContent(json_encode($listId));//透传内容

        //APN高级推送
        Loader::import('getui\igetui\', EXTEND_PATH);
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body=$mes['content'];
        $alertmsg->actionLocKey="查看";
        $alertmsg->locKey=$listId['content'];
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
//         支持$alertmsg->title=$mes['title'];
        $alertmsg->titleLocKey="电力头条";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        $apn->badge=1;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        //PushApn老方式传参
//    $template = new IGtAPNTemplate();
//          $template->set_pushInfo("", 10, "", "", "", "", "", "");return$template;
    }

}

好了,如果前端(android和ios)都以及做好接收处理的话,应该是没问题的了,该案例应用场景于所以下载app的客户,包含注册和未注册的,如果是针对于所有以及注册的用户进行推送的话 建议使用对单个用户进行推送消息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10478.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/10478.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定