xmpushChajian.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. public function setsecret($secret, $package)
  33. {
  34. $this->android_secret = $secret;
  35. $this->android_package = $package;
  36. }
  37. /**
  38. * 安卓推送通知
  39. */
  40. public function androidsend($alias, $title, $cont, $payload='')
  41. {
  42. if(!$alias || !$this->sendbool())return '';
  43. if(is_array($alias))$alias = join(',', $alias);
  44. $data = array(
  45. 'payload' => '',
  46. 'restricted_package_name' => $this->android_package,
  47. 'pass_through' => '0', // 0 表示通知栏消息1表示透传消息
  48. 'title' => $title,
  49. 'description' => $cont,
  50. 'alias' => $alias,
  51. 'notify_type' => '1', //提示语就好了
  52. 'notify_id' => rand(1,45), //可多条显示
  53. 'extra.notify_foreground' => '1',
  54. 'extra.notify_effect' => '1',
  55. );
  56. if($payload)$data['payload'] = urlencode($payload);
  57. return c('curl')->postcurl($this->api_url, $data, 0, array(
  58. 'Authorization'=> 'key='.$this->android_secret
  59. ));
  60. }
  61. //弃用(2021-10-09)
  62. public function iossend($alias, $title, $cont)
  63. {
  64. if(!$alias || $this->ios_secret=='')return '';
  65. if(is_array($alias))$alias = join(',', $alias);
  66. $data = array(
  67. 'title' => $title,
  68. 'aps_proper_fields.title' => $title,
  69. 'description' => $cont,
  70. 'aps_proper_fields.body' => $cont,
  71. 'alias' => $alias,
  72. 'extra.badge'=>'1'
  73. );
  74. return c('curl')->postcurl($this->api_url, $data, 0, array(
  75. 'Authorization'=> 'key='.$this->ios_secret
  76. ));
  77. }
  78. /**
  79. * 推送以下,你用不到不要去管和修改
  80. */
  81. public function jpushiosbool()
  82. {
  83. if($this->jpush_app_key=='')return false;
  84. return true;
  85. }
  86. public function jpushiossend($alias, $title, $cont, $iszs=false)
  87. {
  88. if(!$this->jpushiosbool())return '';
  89. if($this->jpush_ios_ver==1)$iszs = true;
  90. $url = 'https://api.jpush.cn/v3/push';
  91. $body = array();
  92. $base64_auth_string = base64_encode(''.$this->jpush_app_key.':'.$this->jpush_master_secret.'');
  93. $headarr['Authorization'] = 'Basic '.$base64_auth_string.'';
  94. if(is_string($alias))$alias = explode(',', $alias);
  95. $body['platform'] = array('ios');
  96. $body['audience'] = array(
  97. 'alias' => $alias
  98. );
  99. $sound = 'widget/res/sound/sound.aif';
  100. if($cont=='邀请与您视频通话...' || $cont=='邀请与您语音通话...')$sound='widget/res/sound/call.mp3';
  101. $body['notification'] = array(
  102. 'ios' => array(
  103. 'alert' => array(
  104. 'title' => $title,
  105. 'body' => $cont,
  106. ),
  107. 'sound' => $sound,
  108. 'badge' => '+1',
  109. 'thread-id' => 'default',
  110. )
  111. );
  112. $body['options'] = array(
  113. 'time_to_live' => 60,
  114. 'apns_production' => $iszs, //false测试环境
  115. );
  116. $result = c('curl')->postcurl($url, json_encode($body, JSON_UNESCAPED_UNICODE), 0, $headarr);
  117. return $result;
  118. }
  119. }
粤ICP备19079148号