Umeng.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Module;
  3. /**
  4. * Class Umeng
  5. * @package App\Module
  6. */
  7. class Umeng
  8. {
  9. /**
  10. * 推送通知
  11. * @param string $platform ios|android
  12. * @param string $token umeng token
  13. * @param string $title
  14. * @param string $desc
  15. * @param array $extra
  16. * @return array
  17. */
  18. public static function notification($platform, $token, $title, $desc, $extra = [])
  19. {
  20. if ($platform == 'ios') {
  21. $body = [
  22. 'appkey' => env('UMENG_PUSH_IOS_APPKEY'),
  23. 'timestamp' => Base::time(),
  24. 'type' => 'unicast',
  25. 'device_tokens' => $token,
  26. 'payload' => array_merge([
  27. 'aps' => [
  28. 'alert' => [
  29. 'title' => $title,
  30. 'subtitle' => '',
  31. 'body' => $desc,
  32. ]
  33. ],
  34. ], $extra),
  35. ];
  36. } else {
  37. $body = [
  38. 'appkey' => env('UMENG_PUSH_ANDROID_APPKEY'),
  39. 'timestamp' => Base::time(),
  40. 'type' => 'unicast',
  41. 'device_tokens' => $token,
  42. 'payload' => [
  43. 'display_type' => 'notification',
  44. 'body' => [
  45. 'ticker' => $title,
  46. 'title' => $title,
  47. 'text' => $desc,
  48. ],
  49. 'extra' => $extra,
  50. ],
  51. ];
  52. }
  53. //
  54. $res = self::curl($platform, 'https://msgapi.umeng.com/api/send', $body);
  55. if (Base::isError($res)) {
  56. return $res;
  57. } else {
  58. return Base::retSuccess('success');
  59. }
  60. }
  61. /**
  62. * 发送请求
  63. * @param $platform
  64. * @param $url
  65. * @param $body
  66. * @param string $method
  67. * @return array
  68. */
  69. private static function curl($platform, $url, $body, $method = 'POST')
  70. {
  71. if ($platform == 'ios') {
  72. $appkey = env('UMENG_PUSH_IOS_APPKEY');
  73. $secret = env('UMENG_PUSH_IOS_APPMASTERSECRET');
  74. } else {
  75. $appkey = env('UMENG_PUSH_ANDROID_APPKEY');
  76. $secret = env('UMENG_PUSH_ANDROID_APPMASTERSECRET');
  77. }
  78. if (empty($appkey)) {
  79. return Base::retError('no appkey');
  80. }
  81. if (empty($secret)) {
  82. return Base::retError('no secret');
  83. }
  84. //
  85. $postBody = json_encode($body);
  86. $mysign = md5($method . $url . $postBody . $secret);
  87. $url.= "?sign=" . $mysign;
  88. //
  89. $res = Ihttp::ihttp_request($url, $postBody);
  90. if (Base::isError($res)) {
  91. return $res;
  92. }
  93. $array = json_decode($res['data'], true);
  94. $debug = env('UMENG_PUSH_DEBUG');
  95. if ($debug === true || $debug === 'info' || ($debug === 'error' && $array['ret'] !== 'SUCCESS')) {
  96. $logFile = storage_path('logs/umeng-push-' . date('Y-m') . '.log');
  97. file_put_contents($logFile, "[" . date("Y-m-d H:i:s") . "]\n" . Base::array2string_discard([
  98. 'platform' => $platform,
  99. 'url' => $url,
  100. 'method' => $method,
  101. 'body' => $body,
  102. 'request' => $res['data'],
  103. ]) . "\n", FILE_APPEND);
  104. }
  105. if ($array['ret'] == 'SUCCESS') {
  106. return Base::retSuccess('success', $array['data']);
  107. } else {
  108. return Base::retError('error', $array['data']);
  109. }
  110. }
  111. }