push_sender.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. require_once(BASE_DATA_PATH . '/api/notification/android/AndroidCustomizedcast.php');
  3. require_once(BASE_DATA_PATH . '/api/notification/ios/IOSCustomizedcast.php');
  4. require_once(BASE_DATA_PATH . '/api/notification/ios/IOSGroupcast.php');
  5. class push_sender
  6. {
  7. protected $appkey = NULL;
  8. protected $appMasterSecret = NULL;
  9. protected $timestamp = NULL;
  10. protected $validation_token = NULL;
  11. protected $android_alias_type = 'LRLZ';
  12. protected $androd_appkey = '5631efd4e0f55a8770000027';
  13. protected $androd_appmastersecret = 'r6w2a8z9x8zonh7qmk8ds2fvypu02wpj';
  14. protected $ios_alias_type = 'kPandaMakeUpAlias';
  15. protected $ios_appkey = '568b6727e0f55a06580011e7';
  16. protected $ios_appmastersecret = 'q9rmi5zjvfpvkwauenbgvhnmdsqdjeso';
  17. function __construct()
  18. {
  19. $this->timestamp = strval(time());
  20. }
  21. private function sendAndroidCustomizedcast(array $pushinfo)
  22. {
  23. $customizedcast = new AndroidCustomizedcast();
  24. $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
  25. $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
  26. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  27. $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
  28. $customizedcast->setPredefinedKeyValue("after_open", "go_app");
  29. $pre_keys = ['alias','title','text','img'];
  30. foreach ($pushinfo as $key => $value)
  31. {
  32. if(in_array($key,$pre_keys))
  33. {
  34. $customizedcast->setPredefinedKeyValue($key,$value);
  35. if($key == 'title') {
  36. $customizedcast->setPredefinedKeyValue('ticker',$value);
  37. }
  38. }
  39. elseif( $key == 'extra')
  40. {
  41. if(empty($value) || is_array($value) == false) continue;
  42. foreach ($value as $skey => $sval) {
  43. $customizedcast->setExtraField($skey, $sval);
  44. }
  45. }
  46. }
  47. return $customizedcast->send();
  48. }
  49. private function sendIOSCustomizedcast(array $pushinfo)
  50. {
  51. $customizedcast = new IOSCustomizedcast();
  52. $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
  53. $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
  54. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  55. $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
  56. $customizedcast->setPredefinedKeyValue("badge", 0);
  57. $customizedcast->setPredefinedKeyValue("sound", "chime");
  58. if(is_publish()) {
  59. $customizedcast->setPredefinedKeyValue("production_mode", "true");
  60. } else {
  61. $customizedcast->setPredefinedKeyValue("production_mode", "false");
  62. }
  63. $pre_keys = ['alias','text'];
  64. foreach ($pushinfo as $key => $value)
  65. {
  66. if(in_array($key,$pre_keys))
  67. {
  68. if($key == 'text') {
  69. $customizedcast->setPredefinedKeyValue('alert',$value);
  70. } else {
  71. $customizedcast->setPredefinedKeyValue($key,$value);
  72. }
  73. }
  74. elseif( $key == 'extra')
  75. {
  76. if(empty($value) || is_array($value) == false) continue;
  77. foreach ($value as $skey => $sval) {
  78. $customizedcast->setCustomizedField($skey, $sval);
  79. }
  80. }
  81. else {
  82. continue;
  83. }
  84. }
  85. return $customizedcast->send();
  86. }
  87. private function sendAndroidCustomizedcastMessage(array $pushinfo)
  88. {
  89. $customizedcast = new AndroidCustomizedcast();
  90. $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
  91. $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
  92. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  93. $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
  94. $customizedcast->setPredefinedKeyValue("display_type", 'message');
  95. if(is_publish()) {
  96. $customizedcast->setPredefinedKeyValue("production_mode", "true");
  97. } else {
  98. $customizedcast->setPredefinedKeyValue("production_mode", "false");
  99. }
  100. $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
  101. $customizedcast->setPredefinedKeyValue("custom", json_encode($pushinfo['custom']));
  102. return $customizedcast->send();
  103. }
  104. private function sendIOSCustomizedcastMessage(array $pushinfo)
  105. {
  106. $customizedcast = new IOSCustomizedcast();
  107. $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
  108. $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
  109. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  110. $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
  111. $customizedcast->setPredefinedKeyValue("badge", 1);
  112. if(is_publish()) {
  113. $customizedcast->setPredefinedKeyValue("production_mode", "true");
  114. } else {
  115. $customizedcast->setPredefinedKeyValue("production_mode", "false");
  116. }
  117. $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
  118. $customizedcast->setPredefinedKeyValue("alert", $pushinfo['text']);
  119. $customizedcast->setCustomizedField("custom", json_encode($pushinfo['custom']));
  120. return $customizedcast->send();
  121. }
  122. public function send(array $info,$client_type = 0)
  123. {
  124. $fAndroid = false;
  125. $fIos = false;
  126. try
  127. {
  128. if($client_type == 0 || $client_type == session_helper::device_android)
  129. {
  130. $ret = $this->sendAndroidCustomizedcast($info);
  131. $data = json_decode($ret,true);
  132. if(strtoupper($data['ret']) == 'SUCCESS') {
  133. $fAndroid = true;
  134. }
  135. }
  136. }
  137. catch (Exception $ex) {
  138. Log::record("push_app send Android error",Log::DEBUG);
  139. }
  140. try
  141. {
  142. if($client_type == 0 || $client_type == session_helper::device_ios)
  143. {
  144. $ret = $this->sendIOSCustomizedcast($info);
  145. $data = json_decode($ret,true);
  146. if(strtoupper($data['ret']) == 'SUCCESS') {
  147. $fIos = true;
  148. }
  149. }
  150. }
  151. catch (Exception $ex) {
  152. Log::record("push_app send IOS error",Log::ERR);
  153. }
  154. return ($fAndroid | $fIos);
  155. }
  156. public function send_message(array $info,$client_type = 0)
  157. {
  158. $fAndroid = false;
  159. $fIos = false;
  160. try
  161. {
  162. if($client_type == 0 || $client_type == session_helper::device_android)
  163. {
  164. $ret = $this->sendAndroidCustomizedcastMessage($info);
  165. $data = json_decode($ret,true);
  166. if(strtoupper($data['ret']) == 'SUCCESS') {
  167. $fAndroid = true;
  168. }
  169. }
  170. }
  171. catch (Exception $ex) {
  172. Log::record("push_app sendAndroid error",Log::ERR);
  173. }
  174. try
  175. {
  176. if($client_type == 0 || $client_type == session_helper::device_ios)
  177. {
  178. $ret = $this->sendIOSCustomizedcastMessage($info);
  179. $data = json_decode($ret,true);
  180. if(strtoupper($data['ret']) == 'SUCCESS') {
  181. $fIos = true;
  182. }
  183. }
  184. }
  185. catch (Exception $ex) {
  186. Log::record("push_app sendAndroid error",Log::ERR);
  187. }
  188. return ($fAndroid || $fIos);
  189. }
  190. }