xmpushChajian.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * 小米推送
  4. * 参考的开发:https://dev.mi.com/console/doc/detail?pId=1163
  5. */
  6. class xmpushChajian extends Chajian{
  7. private $api_url = 'https://api.xmpush.xiaomi.com/v2/message/alias';
  8. private $api_urltest = 'https://sandbox.xmpush.xiaomi.com/v2/message/alias';
  9. //安卓的
  10. private $android_secret = ''; //安卓小米的secret
  11. private $android_package = ''; //包名
  12. //IOS苹果(弃用不要去设置修改)
  13. private $ios_secret = ''; //IOS的secret
  14. private $ios_bundleid = '';
  15. //2021-10-09最新IOS推送使用极光的,如果自己编译IOS就要配置
  16. private $jpush_app_key = '';
  17. private $jpush_master_secret = '';
  18. private $jpush_ios_ver = ''; //IOS编译的版本1正式版
  19. protected function initChajian()
  20. {
  21. if(!$this->android_secret)$this->android_secret = getconfig('xm_android_secret');
  22. if(!$this->android_package)$this->android_package = getconfig('xm_android_package');
  23. $this->jpush_app_key = getconfig('jpush_app_key');
  24. $this->jpush_master_secret = getconfig('jpush_master_secret');
  25. $this->jpush_ios_ver = getconfig('jpush_ios_ver');
  26. }
  27. public function sendbool()
  28. {
  29. if($this->android_secret=='')return false;
  30. return true;
  31. }
  32. /**
  33. * 安卓推送通知
  34. */
  35. public function androidsend($alias, $title, $cont, $payload='')
  36. {
  37. if(!$alias || !$this->sendbool())return '';
  38. if(is_array($alias))$alias = join(',', $alias);
  39. $data = array(
  40. 'payload' => '',
  41. 'restricted_package_name' => $this->android_package,
  42. 'pass_through' => '0', // 0 表示通知栏消息1表示透传消息
  43. 'title' => $title,
  44. 'description' => $cont,
  45. 'alias' => $alias,
  46. 'notify_type' => '1', //提示语就好了
  47. 'notify_id' => rand(1,45), //可多条显示
  48. 'extra.notify_foreground' => '1',
  49. 'extra.notify_effect' => '1',
  50. );
  51. if($payload)$data['payload'] = urlencode($payload);
  52. return c('curl')->postcurl($this->api_url, $data, 0, array(
  53. 'Authorization'=> 'key='.$this->android_secret
  54. ));
  55. }
  56. //弃用(2021-10-09)
  57. public function iossend($alias, $title, $cont)
  58. {
  59. if(!$alias || $this->ios_secret=='')return '';
  60. if(is_array($alias))$alias = join(',', $alias);
  61. $data = array(
  62. 'title' => $title,
  63. 'aps_proper_fields.title' => $title,
  64. 'description' => $cont,
  65. 'aps_proper_fields.body' => $cont,
  66. 'alias' => $alias,
  67. 'extra.badge'=>'1'
  68. );
  69. return c('curl')->postcurl($this->api_url, $data, 0, array(
  70. 'Authorization'=> 'key='.$this->ios_secret
  71. ));
  72. }
  73. /**
  74. * 推送以下,你用不到不要去管和修改
  75. */
  76. public function jpushiosbool()
  77. {
  78. if($this->jpush_app_key=='')return false;
  79. return true;
  80. }
  81. public function jpushiossend($alias, $title, $cont, $iszs=false)
  82. {
  83. if(!$this->jpushiosbool())return '';
  84. if($this->jpush_ios_ver==1)$iszs = true;
  85. $url = 'https://api.jpush.cn/v3/push';
  86. $body = array();
  87. $base64_auth_string = base64_encode(''.$this->jpush_app_key.':'.$this->jpush_master_secret.'');
  88. $headarr['Authorization'] = 'Basic '.$base64_auth_string.'';
  89. if(is_string($alias))$alias = explode(',', $alias);
  90. $body['platform'] = array('ios');
  91. $body['audience'] = array(
  92. 'alias' => $alias
  93. );
  94. $sound = 'widget/res/sound/sound.aif';
  95. if($cont=='邀请与您视频通话...' || $cont=='邀请与您语音通话...')$sound='widget/res/sound/call.mp3';
  96. $body['notification'] = array(
  97. 'ios' => array(
  98. 'alert' => array(
  99. 'title' => $title,
  100. 'body' => $cont,
  101. ),
  102. 'sound' => $sound,
  103. 'badge' => '+1',
  104. 'thread-id' => 'default',
  105. )
  106. );
  107. $body['options'] = array(
  108. 'time_to_live' => 60,
  109. 'apns_production' => $iszs, //false测试环境
  110. );
  111. $result = c('curl')->postcurl($url, json_encode($body, JSON_UNESCAPED_UNICODE), 0, $headarr);
  112. return $result;
  113. }
  114. }
粤ICP备19079148号