PushTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Console\Commands\Push;
  3. use App\Consts\PushConst;
  4. use App\Libs\Push\HuaWei\HwPushCommon;
  5. use App\Libs\Push\OPPOPush\OPPOPushCommon;
  6. use App\Libs\Push\XMPush\MiPushCommon;
  7. use App\Modules\Push\Models\QappPushApp;
  8. use App\Modules\Push\Models\QappPushUser;
  9. use Exception;
  10. use Illuminate\Console\Command;
  11. class PushTest extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'push:test {uid} {url}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'test push';
  25. /**
  26. * @throws \GuzzleHttp\Exception\GuzzleException
  27. */
  28. public function handle()
  29. {
  30. // 用户Uid
  31. $uid = $this->argument('uid');
  32. $url = $this->argument('url');
  33. // 获取用户push信息
  34. $pushUser = QappPushUser::getPushUserByUid($uid);
  35. $regId = getProp($pushUser, 'reg_id');
  36. $regIdList = [$regId];
  37. // 获取push app信息
  38. $pushAppId = getProp($pushUser, 'app_id');
  39. $pushApp = QappPushApp::getPushAppByAppId($pushAppId);
  40. $package = getProp($pushApp, 'package');
  41. $appId = getProp($pushApp, 'app_id');
  42. $name = getProp($pushApp, 'name');
  43. $provider = getProp($pushApp, 'provider');
  44. $appSecret = getProp($pushApp, 'app_secret');
  45. $appKey = getProp($pushApp, 'app_key');
  46. $masterSecret = getProp($pushApp, 'master_secret');
  47. // 打印
  48. var_dump('name:' . $name);
  49. var_dump('package:' . $package);
  50. var_dump('provider:' . $provider);
  51. var_dump('app_id:' . $appId);
  52. var_dump('app_secret:' . $appSecret);
  53. var_dump('app_key:' . $appKey);
  54. var_dump('master_secret:' . $masterSecret);
  55. $title = '这是' . $name . '标题';
  56. $content = '这是' . $name . '内容,创建于' . date('Y-m-d H:i:s');
  57. $url = $url ?: '/views/Reader';
  58. $params = ['send_order_id' => 1643289, 'bid' => 'vdqY7p15xnZQzK4VzzgmMD6wG2yr8BNX', 'chapter_id' => 4774851];
  59. var_dump('uid:' . $uid);
  60. var_dump('title:' . $title);
  61. var_dump('content:' . $content);
  62. var_dump('url:' . $url);
  63. var_dump('params:' . json_encode($params));
  64. var_dump('reg id:' . $regId);
  65. $result = [];
  66. try {
  67. switch ($provider) {
  68. // 华为
  69. case PushConst::PROVIDER_HW:
  70. // 初始化huawei推送
  71. $client = new HwPushCommon($appId, $appSecret);
  72. // 循环推送
  73. $client->setToken($regIdList);
  74. $result = $client->sendPushMessage($title, $content, $url, $params);
  75. break;
  76. // 小米
  77. case PushConst::PROVIDER_MI:
  78. // 初始化小米推送
  79. $client = new MiPushCommon($package, $appSecret);
  80. // 循环推送
  81. $client->setRegArr($regIdList);
  82. $result = $client->sendMessage($title, $content, $url);
  83. break;
  84. // OPPO
  85. case PushConst::PROVIDER_OPPO:
  86. // 初始化oppo推送
  87. $client = new OPPOPushCommon($appKey, $masterSecret);
  88. $messageId = $client->getMessageId($title, $content, $url, $params);
  89. // 循环推送
  90. $client->setRegArr($regIdList);
  91. $result = $client->broadCastRegIds($messageId);
  92. break;
  93. }
  94. } catch (Exception $e) {
  95. var_dump($e->getMessage());
  96. }
  97. dd($result);
  98. }
  99. }