hwpushChajian.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * 来自:信呼开发团队
  4. * 作者:磐石(rainrock)
  5. * 网址:http://www.rockoa.com/
  6. * 华为的推送服务
  7. * 参考的开发:https://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_v3.html?page=hmssdk_huaweipush_api_reference_messagesend
  8. */
  9. class hwpushChajian extends Chajian{
  10. //华为推送的设置【自己编译app】以下2个必须设置
  11. private $appid = '';
  12. private $appsecret = '';
  13. /**
  14. * 相关域名定义
  15. */
  16. private $tokenurl = 'https://oauth-login.cloud.huawei.com/oauth2/v3/token';
  17. private $pushurl = 'https://push-api.cloud.huawei.com/v1/{appid}/messages:send';
  18. protected function initChajian()
  19. {
  20. if(!$this->appid)$this->appid = getconfig('hw_appid');
  21. if(!$this->appsecret)$this->appsecret = getconfig('hw_appsecret');
  22. }
  23. public function sendbool()
  24. {
  25. if($this->appsecret=='')return false;
  26. return true;
  27. }
  28. public function setinfo($apid,$apse)
  29. {
  30. $this->appid = $apid;
  31. $this->appsecret = $apse;
  32. return $this;
  33. }
  34. /**
  35. * 获取token
  36. */
  37. public function gettoken(){
  38. if($this->appid=='' || $this->appsecret=='')return '';
  39. $token = c('cache')->get('hwtoken');
  40. if(isempt($token)){
  41. $result = c('curl')->postcurl($this->tokenurl, array(
  42. "grant_type" => "client_credentials",
  43. "client_secret" => $this->appsecret,
  44. "client_id" => $this->appid,
  45. ));
  46. if($result){
  47. $barr = json_decode($result, true);
  48. $token = $barr['access_token'];
  49. c('cache')->set('hwtoken',$token, $barr['expires_in']-1);
  50. }
  51. }
  52. return $token;
  53. }
  54. /**
  55. * 安卓推送通知
  56. */
  57. public function androidsend($alias, $title, $cont)
  58. {
  59. if(!$this->sendbool())return '';
  60. $url = str_replace('{appid}',$this->appid, $this->pushurl);
  61. $token = $this->gettoken();
  62. if(!$token)return '';
  63. $data = '{"validate_only": false, "message": {"android": { "notification": { "title": "'.$title.'", "body": "'.$cont.'", "click_action": { "type": 1, "intent": "#Intent;compo=com.rvr/.Activity;S.W=U;end" } } }, "token": '.json_encode($alias).' }}';
  64. $result = c('curl')->postcurl($url, $data, 0, array(
  65. "Content-Type" => 'application/json',
  66. "Authorization" => "Bearer {$token}"
  67. ));
  68. return $result;
  69. }
  70. }
粤ICP备19079148号