You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

701 lines
24 KiB

  1. <?php
  2. namespace JPush;
  3. use InvalidArgumentException;
  4. class PushPayload {
  5. private static $EFFECTIVE_DEVICE_TYPES = array('ios', 'android', 'winphone');
  6. const PUSH_URL = 'https://api.jpush.cn/v3/push';
  7. const PUSH_VALIDATE_URL = 'https://api.jpush.cn/v3/push/validate';
  8. private $client;
  9. private $platform;
  10. private $audience;
  11. private $tags;
  12. private $tagAnds;
  13. private $alias;
  14. private $registrationIds;
  15. private $notificationAlert;
  16. private $iosNotification;
  17. private $androidNotification;
  18. private $winPhoneNotification;
  19. private $smsMessage;
  20. private $message;
  21. private $options;
  22. /**
  23. * PushPayload constructor.
  24. * @param $client JPush
  25. */
  26. function __construct($client) {
  27. $this->client = $client;
  28. }
  29. public function setPlatform($platform) {
  30. # $required_keys = array('all', 'android', 'ios', 'winphone');
  31. if (is_string($platform)) {
  32. $ptf = strtolower($platform);
  33. if ('all' === $ptf) {
  34. $this->platform = 'all';
  35. } elseif (in_array($ptf, self::$EFFECTIVE_DEVICE_TYPES)) {
  36. $this->platform = array($ptf);
  37. }
  38. } elseif (is_array($platform)) {
  39. $ptf = array_map('strtolower', $platform);
  40. $this->platform = array_intersect($ptf, self::$EFFECTIVE_DEVICE_TYPES);
  41. }
  42. return $this;
  43. }
  44. public function setAudience($all) {
  45. if (strtolower($all) === 'all') {
  46. $this->addAllAudience();
  47. return $this;
  48. } else {
  49. throw new InvalidArgumentException('Invalid audience value');
  50. }
  51. }
  52. public function addAllAudience() {
  53. $this->audience = "all";
  54. return $this;
  55. }
  56. public function addTag($tag) {
  57. if (is_null($this->tags)) {
  58. $this->tags = array();
  59. }
  60. if (is_array($tag)) {
  61. foreach($tag as $_tag) {
  62. if (!is_string($_tag)) {
  63. throw new InvalidArgumentException("Invalid tag value");
  64. }
  65. if (!in_array($_tag, $this->tags)) {
  66. array_push($this->tags, $_tag);
  67. }
  68. }
  69. } else if (is_string($tag)) {
  70. if (!in_array($tag, $this->tags)) {
  71. array_push($this->tags, $tag);
  72. }
  73. } else {
  74. throw new InvalidArgumentException("Invalid tag value");
  75. }
  76. return $this;
  77. }
  78. public function addTagAnd($tag) {
  79. if (is_null($this->tagAnds)) {
  80. $this->tagAnds = array();
  81. }
  82. if (is_array($tag)) {
  83. foreach($tag as $_tag) {
  84. if (!is_string($_tag)) {
  85. throw new InvalidArgumentException("Invalid tag_and value");
  86. }
  87. if (!in_array($_tag, $this->tagAnds)) {
  88. array_push($this->tagAnds, $_tag);
  89. }
  90. }
  91. } else if (is_string($tag)) {
  92. if (!in_array($tag, $this->tagAnds)) {
  93. array_push($this->tagAnds, $tag);
  94. }
  95. } else {
  96. throw new InvalidArgumentException("Invalid tag_and value");
  97. }
  98. return $this;
  99. }
  100. public function addAlias($alias) {
  101. if (is_null($this->alias)) {
  102. $this->alias = array();
  103. }
  104. if (is_array($alias)) {
  105. foreach($alias as $_alias) {
  106. if (!is_string($_alias)) {
  107. throw new InvalidArgumentException("Invalid alias value");
  108. }
  109. if (!in_array($_alias, $this->alias)) {
  110. array_push($this->alias, $_alias);
  111. }
  112. }
  113. } else if (is_string($alias)) {
  114. if (!in_array($alias, $this->alias)) {
  115. array_push($this->alias, $alias);
  116. }
  117. } else {
  118. throw new InvalidArgumentException("Invalid alias value");
  119. }
  120. return $this;
  121. }
  122. public function addRegistrationId($registrationId) {
  123. if (is_null($this->registrationIds)) {
  124. $this->registrationIds = array();
  125. }
  126. if (is_array($registrationId)) {
  127. foreach($registrationId as $_registrationId) {
  128. if (!is_string($_registrationId)) {
  129. throw new InvalidArgumentException("Invalid registration_id value");
  130. }
  131. if (!in_array($_registrationId, $this->registrationIds)) {
  132. array_push($this->registrationIds, $_registrationId);
  133. }
  134. }
  135. } else if (is_string($registrationId)) {
  136. if (!in_array($registrationId, $this->registrationIds)) {
  137. array_push($this->registrationIds, $registrationId);
  138. }
  139. } else {
  140. throw new InvalidArgumentException("Invalid registration_id value");
  141. }
  142. return $this;
  143. }
  144. public function setNotificationAlert($alert) {
  145. if (!is_string($alert)) {
  146. throw new InvalidArgumentException("Invalid alert value");
  147. }
  148. $this->notificationAlert = $alert;
  149. return $this;
  150. }
  151. public function addWinPhoneNotification($alert=null, $title=null, $_open_page=null, $extras=null) {
  152. $winPhone = array();
  153. if (!is_null($alert)) {
  154. if (!is_string($alert)) {
  155. throw new InvalidArgumentException("Invalid winphone notification");
  156. }
  157. $winPhone['alert'] = $alert;
  158. }
  159. if (!is_null($title)) {
  160. if (!is_string($title)) {
  161. throw new InvalidArgumentException("Invalid winphone title notification");
  162. }
  163. if(strlen($title) > 0) {
  164. $winPhone['title'] = $title;
  165. }
  166. }
  167. if (!is_null($_open_page)) {
  168. if (!is_string($_open_page)) {
  169. throw new InvalidArgumentException("Invalid winphone _open_page notification");
  170. }
  171. if (strlen($_open_page) > 0) {
  172. $winPhone['_open_page'] = $_open_page;
  173. }
  174. }
  175. if (!is_null($extras)) {
  176. if (!is_array($extras)) {
  177. throw new InvalidArgumentException("Invalid winphone extras notification");
  178. }
  179. if (count($extras) > 0) {
  180. $winPhone['extras'] = $extras;
  181. }
  182. }
  183. if (count($winPhone) <= 0) {
  184. throw new InvalidArgumentException("Invalid winphone notification");
  185. }
  186. $this->winPhoneNotification = $winPhone;
  187. return $this;
  188. }
  189. public function setSmsMessage($content, $delay_time = 0) {
  190. $sms = array();
  191. if (is_string($content) && mb_strlen($content) < 480) {
  192. $sms['content'] = $content;
  193. } else {
  194. throw new InvalidArgumentException('Invalid sms content, sms content\'s length must in [0, 480]');
  195. }
  196. $sms['delay_time'] = ($delay_time === 0 || (is_int($delay_time) && $delay_time > 0 && $delay_time <= 86400)) ? $delay_time : 0;
  197. $this->smsMessage = $sms;
  198. return $this;
  199. }
  200. public function build() {
  201. $payload = array();
  202. // validate platform
  203. if (is_null($this->platform)) {
  204. throw new InvalidArgumentException("platform must be set");
  205. }
  206. $payload["platform"] = $this->platform;
  207. // validate audience
  208. $audience = array();
  209. if (!is_null($this->tags)) {
  210. $audience["tag"] = $this->tags;
  211. }
  212. if (!is_null($this->tagAnds)) {
  213. $audience["tag_and"] = $this->tagAnds;
  214. }
  215. if (!is_null($this->alias)) {
  216. $audience["alias"] = $this->alias;
  217. }
  218. if (!is_null($this->registrationIds)) {
  219. $audience["registration_id"] = $this->registrationIds;
  220. }
  221. if (is_null($this->audience) && count($audience) <= 0) {
  222. throw new InvalidArgumentException("audience must be set");
  223. } else if (!is_null($this->audience) && count($audience) > 0) {
  224. throw new InvalidArgumentException("you can't add tags/alias/registration_id/tag_and when audience='all'");
  225. } else if (is_null($this->audience)) {
  226. $payload["audience"] = $audience;
  227. } else {
  228. $payload["audience"] = $this->audience;
  229. }
  230. // validate notification
  231. $notification = array();
  232. if (!is_null($this->notificationAlert)) {
  233. $notification['alert'] = $this->notificationAlert;
  234. }
  235. if (!is_null($this->androidNotification)) {
  236. $notification['android'] = $this->androidNotification;
  237. if (is_null($this->androidNotification['alert'])) {
  238. if (is_null($this->notificationAlert)) {
  239. throw new InvalidArgumentException("Android alert can not be null");
  240. } else {
  241. $notification['android']['alert'] = $this->notificationAlert;
  242. }
  243. }
  244. }
  245. if (!is_null($this->iosNotification)) {
  246. $notification['ios'] = $this->iosNotification;
  247. if (is_null($this->iosNotification['alert'])) {
  248. if (is_null($this->notificationAlert)) {
  249. throw new InvalidArgumentException("iOS alert can not be null");
  250. } else {
  251. $notification['ios']['alert'] = $this->notificationAlert;
  252. }
  253. }
  254. }
  255. if (!is_null($this->winPhoneNotification)) {
  256. $notification['winphone'] = $this->winPhoneNotification;
  257. if (is_null($this->winPhoneNotification['alert'])) {
  258. if (is_null($this->winPhoneNotification)) {
  259. throw new InvalidArgumentException("WinPhone alert can not be null");
  260. } else {
  261. $notification['winphone']['alert'] = $this->notificationAlert;
  262. }
  263. }
  264. }
  265. if (count($notification) > 0) {
  266. $payload['notification'] = $notification;
  267. }
  268. if (count($this->message) > 0) {
  269. $payload['message'] = $this->message;
  270. }
  271. if (!array_key_exists('notification', $payload) && !array_key_exists('message', $payload)) {
  272. throw new InvalidArgumentException('notification and message can not all be null');
  273. }
  274. if (count($this->smsMessage)) {
  275. $payload['sms_message'] = $this->smsMessage;
  276. }
  277. if (count($this->options) <= 0) {
  278. $this->options(array('apns_production' => false));
  279. }
  280. $payload['options'] = $this->options;
  281. return $payload;
  282. }
  283. public function toJSON() {
  284. $payload = $this->build();
  285. $this->clearAll();
  286. return json_encode($payload);
  287. }
  288. public function printJSON() {
  289. echo $this->toJSON();
  290. return $this;
  291. }
  292. public function send() {
  293. $url = PushPayload::PUSH_URL;
  294. return Http::post($this->client, $url, $this->toJSON());
  295. }
  296. public function validate() {
  297. $url = PushPayload::PUSH_VALIDATE_URL;
  298. return Http::post($this->client, $url, $this->toJSON());
  299. }
  300. public function clearAudience() {
  301. $this->audience = null;
  302. $this->tags = null;
  303. $this->tagAnds = null;
  304. $this->alias = null;
  305. $this->registrationIds = null;
  306. }
  307. public function clearNotification() {
  308. $this->notificationAlert = null;
  309. $this->iosNotification = null;
  310. $this->androidNotification = null;
  311. $this->winPhoneNotification = null;
  312. }
  313. public function clearPlatform() { $this->platform = null; }
  314. public function clearMessage() { $this->message = null; }
  315. public function clearSmsMessage() { $this->smsMessage = null; }
  316. public function clearOptions() { $this->options = null; }
  317. public function clearAll() {
  318. $this->clearPlatform();
  319. $this->clearAudience();
  320. $this->clearNotification();
  321. $this->clearMessage();
  322. $this->clearSmsMessage();
  323. $this->clearOptions();
  324. }
  325. private function generateSendno() {
  326. return rand(100000, 4294967294);
  327. }
  328. # new methods
  329. public function iosNotification($alert = '', array $notification = array()) {
  330. # $required_keys = array('sound', 'badge', 'content-available', 'mutable-content', category', 'extras');
  331. $ios = array();
  332. $ios['alert'] = (is_string($alert) || is_array($alert)) ? $alert : '';
  333. if (!empty($notification)) {
  334. if (isset($notification['sound']) && is_string($notification['sound'])) {
  335. $ios['sound'] = $notification['sound'];
  336. }
  337. if (isset($notification['badge'])) {
  338. $ios['badge'] = (int)$notification['badge'] ? $notification['badge'] : 0;
  339. }
  340. if (isset($notification['content-available']) && is_bool($notification['content-available']) && $notification['content-available']) {
  341. $ios['content-available'] = $notification['content-available'];
  342. }
  343. if (isset($notification['mutable-content']) && is_bool($notification['mutable-content']) && $notification['mutable-content']) {
  344. $ios['mutable-content'] = $notification['mutable-content'];
  345. }
  346. if (isset($notification['category']) && is_string($notification['category'])) {
  347. $ios['category'] = $notification['category'];
  348. }
  349. if (isset($notification['extras']) && is_array($notification['extras']) && !empty($notification['extras'])) {
  350. $ios['extras'] = $notification['extras'];
  351. }
  352. }
  353. if (!isset($ios['sound'])) {
  354. $ios['sound'] = '';
  355. }
  356. if (!isset($ios['badge'])) {
  357. $ios['badge'] = '+1';
  358. }
  359. $this->iosNotification = $ios;
  360. return $this;
  361. }
  362. public function androidNotification($alert = '', array $notification = array()) {
  363. # $required_keys = array('title', 'build_id', 'extras');
  364. $android = array();
  365. $android['alert'] = is_string($alert) ? $alert : '';
  366. if (!empty($notification)) {
  367. if (isset($notification['title']) && is_string($notification['title'])) {
  368. $android['title'] = $notification['title'];
  369. }
  370. if (isset($notification['build_id']) && is_int($notification['build_id'])) {
  371. $android['build_id'] = $notification['build_id'];
  372. }
  373. if (isset($notification['extras']) && is_array($notification['extras']) && !empty($notification['extras'])) {
  374. $android['extras'] = $notification['extras'];
  375. }
  376. if (isset($notification['priority']) && is_int($notification['priority'])) {
  377. $android['priority'] = $notification['priority'];
  378. }
  379. if (isset($notification['category']) && is_string($notification['category'])) {
  380. $android['category'] = $notification['category`'];
  381. }
  382. if (isset($notification['style']) && is_int($notification['style'])) {
  383. $android['style'] = $notification['style'];
  384. }
  385. if (isset($notification['big_text']) && is_string($notification['big_text'])) {
  386. $android['big_text'] = $notification['big_text'];
  387. }
  388. if (isset($notification['inbox']) && is_array($notification['inbox'])) {
  389. $android['inbox'] = $notification['inbox'];
  390. }
  391. if (isset($notification['big_pic_path']) && is_string($notification['big_pic_path'])) {
  392. $android['big_pic_path'] = $notification['big_pic_path'];
  393. }
  394. }
  395. $this->androidNotification = $android;
  396. return $this;
  397. }
  398. public function message($msg_content, array $msg = array()) {
  399. # $required_keys = array('title', 'content_type', 'extras');
  400. if (is_string($msg_content)) {
  401. $message = array();
  402. $message['msg_content'] = $msg_content;
  403. if (!empty($msg)) {
  404. if (isset($msg['title']) && is_string($msg['title'])) {
  405. $message['title'] = $msg['title'];
  406. }
  407. if (isset($msg['content_type']) && is_string($msg['content_type'])) {
  408. $message['content_type'] = $msg['content_type'];
  409. }
  410. if (isset($msg['extras']) && is_array($msg['extras']) && !empty($msg['extras'])) {
  411. $message['extras'] = $msg['extras'];
  412. }
  413. }
  414. $this->message = $message;
  415. }
  416. return $this;
  417. }
  418. public function options(array $opts = array()) {
  419. # $required_keys = array('sendno', 'time_to_live', 'override_msg_id', 'apns_production', 'big_push_duration');
  420. if (!empty($opts)) {
  421. $options = array();
  422. if (isset($opts['sendno']) && is_int($opts['sendno'])) {
  423. $options['sendno'] = $opts['sendno'];
  424. }
  425. if (isset($opts['time_to_live']) && is_int($opts['time_to_live']) && $opts['time_to_live'] <= 864000 && $opts['time_to_live'] >= 0) {
  426. $options['time_to_live'] = $opts['time_to_live'];
  427. }
  428. if (isset($opts['override_msg_id']) && is_long($opts['override_msg_id'])) {
  429. $options['override_msg_id'] = $opts['override_msg_id'];
  430. }
  431. if (isset($opts['apns_production']) && is_bool($opts['apns_production'])) {
  432. $options['apns_production'] = $opts['apns_production'];
  433. } else {
  434. $options['apns_production'] = false;
  435. }
  436. if (isset($opts['big_push_duration']) && is_int($opts['big_push_duration']) && $opts['big_push_duration'] <= 1400 && $opts['big_push_duration'] >= 0) {
  437. $options['big_push_duration'] = $opts['big_push_duration'];
  438. }
  439. $this->options = $options;
  440. }
  441. return $this;
  442. }
  443. ###############################################################################
  444. ############# 以下函数已过期,不推荐使用,仅作为兼容接口存在 #########################
  445. ###############################################################################
  446. public function addIosNotification($alert=null, $sound=null, $badge=null, $content_available=null, $category=null, $extras=null) {
  447. $ios = array();
  448. if (!is_null($alert)) {
  449. if (!is_string($alert) && !is_array($alert)) {
  450. throw new InvalidArgumentException("Invalid ios alert value");
  451. }
  452. $ios['alert'] = $alert;
  453. }
  454. if (!is_null($sound)) {
  455. if (!is_string($sound)) {
  456. throw new InvalidArgumentException("Invalid ios sound value");
  457. }
  458. if ($sound !== Config::DISABLE_SOUND) {
  459. $ios['sound'] = $sound;
  460. }
  461. } else {
  462. // 默认sound为''
  463. $ios['sound'] = '';
  464. }
  465. if (!is_null($badge)) {
  466. if (is_string($badge) && !preg_match("/^[+-]{1}[0-9]{1,3}$/", $badge)) {
  467. if (!is_int($badge)) {
  468. throw new InvalidArgumentException("Invalid ios badge value");
  469. }
  470. }
  471. if ($badge != Config::DISABLE_BADGE) {
  472. $ios['badge'] = $badge;
  473. }
  474. } else {
  475. // 默认badge为'+1'
  476. $ios['badge'] = '+1';
  477. }
  478. if (!is_null($content_available)) {
  479. if (!is_bool($content_available)) {
  480. throw new InvalidArgumentException("Invalid ios content-available value");
  481. }
  482. $ios['content-available'] = $content_available;
  483. }
  484. if (!is_null($category)) {
  485. if (!is_string($category)) {
  486. throw new InvalidArgumentException("Invalid ios category value");
  487. }
  488. if (strlen($category)) {
  489. $ios['category'] = $category;
  490. }
  491. }
  492. if (!is_null($extras)) {
  493. if (!is_array($extras)) {
  494. throw new InvalidArgumentException("Invalid ios extras value");
  495. }
  496. if (count($extras) > 0) {
  497. $ios['extras'] = $extras;
  498. }
  499. }
  500. if (count($ios) <= 0) {
  501. throw new InvalidArgumentException("Invalid iOS notification");
  502. }
  503. $this->iosNotification = $ios;
  504. return $this;
  505. }
  506. public function addAndroidNotification($alert=null, $title=null, $builderId=null, $extras=null) {
  507. $android = array();
  508. if (!is_null($alert)) {
  509. if (!is_string($alert)) {
  510. throw new InvalidArgumentException("Invalid android alert value");
  511. }
  512. $android['alert'] = $alert;
  513. }
  514. if (!is_null($title)) {
  515. if(!is_string($title)) {
  516. throw new InvalidArgumentException("Invalid android title value");
  517. }
  518. if(strlen($title) > 0) {
  519. $android['title'] = $title;
  520. }
  521. }
  522. if (!is_null($builderId)) {
  523. if (!is_int($builderId)) {
  524. throw new InvalidArgumentException("Invalid android builder_id value");
  525. }
  526. $android['builder_id'] = $builderId;
  527. }
  528. if (!is_null($extras)) {
  529. if (!is_array($extras)) {
  530. throw new InvalidArgumentException("Invalid android extras value");
  531. }
  532. if (count($extras) > 0) {
  533. $android['extras'] = $extras;
  534. }
  535. }
  536. if (count($android) <= 0) {
  537. throw new InvalidArgumentException("Invalid android notification");
  538. }
  539. $this->androidNotification = $android;
  540. return $this;
  541. }
  542. public function setMessage($msg_content, $title=null, $content_type=null, $extras=null) {
  543. $message = array();
  544. if (is_null($msg_content) || !is_string($msg_content)) {
  545. throw new InvalidArgumentException("Invalid message content");
  546. } else {
  547. $message['msg_content'] = $msg_content;
  548. }
  549. if (!is_null($title)) {
  550. if (!is_string($title)) {
  551. throw new InvalidArgumentException("Invalid message title");
  552. }
  553. $message['title'] = $title;
  554. }
  555. if (!is_null($content_type)) {
  556. if (!is_string($content_type)) {
  557. throw new InvalidArgumentException("Invalid message content type");
  558. }
  559. $message["content_type"] = $content_type;
  560. }
  561. if (!is_null($extras)) {
  562. if (!is_array($extras)) {
  563. throw new InvalidArgumentException("Invalid message extras");
  564. }
  565. if (count($extras) > 0) {
  566. $message['extras'] = $extras;
  567. }
  568. }
  569. $this->message = $message;
  570. return $this;
  571. }
  572. public function setOptions($sendno=null, $time_to_live=null, $override_msg_id=null, $apns_production=null, $big_push_duration=null) {
  573. $options = array();
  574. if (!is_null($sendno)) {
  575. if (!is_int($sendno)) {
  576. throw new InvalidArgumentException('Invalid option sendno');
  577. }
  578. $options['sendno'] = $sendno;
  579. } else {
  580. $options['sendno'] = $this->generateSendno();
  581. }
  582. if (!is_null($time_to_live)) {
  583. if (!is_int($time_to_live) || $time_to_live < 0 || $time_to_live > 864000) {
  584. throw new InvalidArgumentException('Invalid option time to live, it must be a int and in [0, 864000]');
  585. }
  586. $options['time_to_live'] = $time_to_live;
  587. }
  588. if (!is_null($override_msg_id)) {
  589. if (!is_long($override_msg_id)) {
  590. throw new InvalidArgumentException('Invalid option override msg id');
  591. }
  592. $options['override_msg_id'] = $override_msg_id;
  593. }
  594. if (!is_null($apns_production)) {
  595. if (!is_bool($apns_production)) {
  596. throw new InvalidArgumentException('Invalid option apns production');
  597. }
  598. $options['apns_production'] = $apns_production;
  599. } else {
  600. $options['apns_production'] = false;
  601. }
  602. if (!is_null($big_push_duration)) {
  603. if (!is_int($big_push_duration) || $big_push_duration < 0 || $big_push_duration > 1440) {
  604. throw new InvalidArgumentException('Invalid option big push duration, it must be a int and in [0, 1440]');
  605. }
  606. $options['big_push_duration'] = $big_push_duration;
  607. }
  608. $this->options = $options;
  609. return $this;
  610. }
  611. }