极光推送

首先.去极光的官方文档https://docs.jiguang.cn/下载极光推送的类!

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

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

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

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

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

 

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

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

  1. <?php  
  2.   
  3. use JPush\Client as JPush;  
  4. class Common {  
  5.   
  6.    //极光推送appkey  
  7.     static public function app_key(){  
  8.   
  9.         $app_key = "极光账号的app_key";  
  10.         return $app_key;  
  11.     }  
  12.     //极光推送master_secret  
  13.     static public function master_secret(){  
  14.   
  15.         $master_secret = "极光账号的master_secret";  
  16.         return $master_secret;  
  17.     }  
  18.     //获取alias和tags  
  19.     public function getDevices($registrationID){  
  20.   
  21.         require_once APP_PATH . '/../extend/jpush/autoload.php';   
  22.   
  23.         $app_key = $this->app_key();  
  24.         $master_secret = $this->master_secret();  
  25.   
  26.         $client = new JPush($app_key, $master_secret);  
  27.   
  28.         $result = $client->device()->getDevices($registrationID);  
  29.           
  30.         return $result;  
  31.   
  32.     }  
  33.     //添加tags  
  34.     public function addTags($registrationID,$tags){  
  35.   
  36.         require_once APP_PATH . '/../extend/jpush/autoload.php';   
  37.   
  38.         $app_key = $this->app_key();  
  39.         $master_secret = $this->master_secret();  
  40.   
  41.         $client = new JPush($app_key, $master_secret);  
  42.   
  43.         $result = $client->device()->addTags($registrationID,$tags);  
  44.           
  45.         return $result;  
  46.   
  47.     }  
  48.   
  49.     //移除tags  
  50.     public function removeTags($registrationID,$tags){  
  51.   
  52.         require_once APP_PATH . '/../extend/jpush/autoload.php';   
  53.   
  54.         $app_key = $this->app_key();  
  55.         $master_secret = $this->master_secret();  
  56.   
  57.         $client = new JPush($app_key, $master_secret);  
  58.   
  59.         $result = $client->device()->removeTags($registrationID,$tags);  
  60.           
  61.         return $result;  
  62.   
  63.     }  
  64.     //标签推送  
  65.     public function push($tag,$alert){  
  66.   
  67.         require_once APP_PATH . '/../extend/jpush/autoload.php';   
  68.   
  69.         $app_key = $this->app_key();  
  70.         $master_secret = $this->master_secret();  
  71.   
  72.         $client = new JPush($app_key, $master_secret);  
  73.   
  74.         $tags = implode(",",$tag);  
  75.   
  76.         $client->push()  
  77.                 ->setPlatform(array('ios', 'android'))  
  78.                 ->addTag($tags)                          //标签  
  79.                 ->setNotificationAlert($alert)           //内容  
  80.                 ->send();  
  81.   
  82.     }  
  83.   
  84.     //别名推送  
  85.     public function push_a($alias,$alert){  
  86.   
  87.         require_once APP_PATH . '/../extend/jpush/autoload.php';   
  88.   
  89.         $app_key = $this->app_key();  
  90.         $master_secret = $this->master_secret();  
  91.   
  92.         $client = new JPush($app_key, $master_secret);  
  93.   
  94.         $alias = implode(",",$alias);  
  95.   
  96.         $client->push()  
  97.                 ->setPlatform(array('ios', 'android'))  
  98.                 ->addAlias($alias)                      //别名  
  99.                 ->setNotificationAlert($alert)          //内容  
  100.                 ->send();  
  101.   
  102.     }  
  103. }  

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

 

简单引用:

 

[php] view plain copy
 
  1. $common = new Common();  
  2. $tag = array(  
  3.                "123"  
  4.              );  
  5. $alert = "标签推送";  
  6. $common->push($tag,$alert);  

封装好后引用就这么些代码!到这里推送就可以成功了!

posted @ 2017-08-28 11:21  沧海一粒沙  阅读(423)  评论(0编辑  收藏  举报