您的位置:首页 > 编程语言 > PHP开发

[置顶] php极光推送详解过程

2017-04-27 10:31 246 查看
首先.去极光的官方文档https://docs.jiguang.cn/下载极光推送的类!

下载后放到你的公共目录下!

放好后,我先来说一下关于推送这方面需要用到的东西!!

1:别名(alias)),首先别名是一个相当于给每一个用户标识的名字,一个用户只能有一个别名而且是唯一的,如果你换了其他的别名,当前别名会覆盖!

2:标签(tags),标签.所谓的推送标签,是可以给每个用户立一个或多个的标签!! 就相当于QQ群一样,你在每个群的标识都是唯一的,不可重复.这样如果叫到你的名字标识的时候.就可以直接认定是你这个人!

3:registrationID,这个ID是每部手机的设备号id,每一个手机只有一个并且是唯一!

registrationID和tags必须保存到数据库!因为registrationID是设备号id,每一次登录的时候要更新一下数据库的registrationID!tags是标签,每次给用户立标签的时候都得在数据库更新一遍标签! 对于那个别名,最好是用用户的ID,我这边是,如果你想自己立也可以!! 不过用户的ID也是唯一的,所以用用户的id的话就不用给这个别名建立一个字段了,所以这方面会省很多事!!

下边是我自己封装的一个小小的类方法!

<?php

use JPush\Client as JPush;
class Common {

//极光推送appkey
static public function app_key(){

$app_key = "极光账号的app_key";
return $app_key;
}
//极光推送master_secret
static public function master_secret(){

$master_secret = "极光账号的master_secret";
return $master_secret;
}
//获取alias和tags
public function getDevices($registrationID){

require_once APP_PATH . '/../extend/jpush/autoload.php';

$app_key = $this->app_key();
$master_secret = $this->master_secret();

$client = new JPush($app_key, $master_secret);

$result = $client->device()->getDevices($registrationID);

return $result;

}
//添加tags
public function addTags($registrationID,$tags){

require_once APP_PATH . '/../extend/jpush/autoload.php';

$app_key = $this->app_key();
$master_secret = $this->master_secret();

$client = new JPush($app_key, $master_secret);

$result = $client->device()->addTags($registrationID,$tags);

return $result;

}

//移除tags
public function removeTags($registrationID,$tags){

require_once APP_PATH . '/../extend/jpush/autoload.php';

$app_key = $this->app_key();
$master_secret = $this->master_secret();

$client = new JPush($app_key, $master_secret);

$result = $client->device()->removeTags($registrationID,$tags);

return $result;

}
//标签推送
public function push($tag,$alert){

require_once APP_PATH . '/../extend/jpush/autoload.php';

$app_key = $this->app_key();
$master_secret = $this->master_secret();

$client = new JPush($app_key, $master_secret);

$tags = implode(",",$tag);

$client->push()
->setPlatform(array('ios', 'android'))
->addTag($tags)                          //标签
->setNotificationAlert($alert)           //内容
->send();

}

//别名推送
public function push_a($alias,$alert){

require_once APP_PATH . '/../extend/jpush/autoload.php';

$app_key = $this->app_key();
$master_secret = $this->master_secret();

$client = new JPush($app_key, $master_secret);

$alias = implode(",",$alias);

$client->push()
->setPlatform(array('ios', 'android'))
->addAlias($alias)                      //别名
->setNotificationAlert($alert)          //内容
->send();

}
}
上边是我封装的一个小类,就是简单的给用户立标签,获取用户的标签,标签推送和别名推送!

简单引用:

$common = new Common();
$tag = array(
"123"
);
$alert = "标签推送";
$common->push($tag,$alert);
封装好后引用就这么些代码!到这里推送就可以成功了!

这就是简单的PHP推送! 如有不懂的可以评论!帮到忙的点个赞 ~ 谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: