feat(develop): 屏蔽极光推送
Showing
2 changed files
with
1 additions
and
119 deletions
| ... | @@ -2,23 +2,14 @@ | ... | @@ -2,23 +2,14 @@ |
| 2 | 2 | ||
| 3 | namespace App\Channels; | 3 | namespace App\Channels; |
| 4 | 4 | ||
| 5 | use App\Jobs\JPushMessageJob; | ||
| 6 | use App\Models\User; | 5 | use App\Models\User; |
| 7 | use Illuminate\Notifications\Notification; | 6 | use Illuminate\Notifications\Notification; |
| 8 | use Illuminate\Support\Arr; | ||
| 9 | 7 | ||
| 10 | class JPushNotificationChannel | 8 | class JPushNotificationChannel |
| 11 | { | 9 | { |
| 12 | public function send(User $notifiable, Notification $notification): void | 10 | public function send(User $notifiable, Notification $notification): void |
| 13 | { | 11 | { |
| 14 | // @phpstan-ignore-next-line | 12 | // @phpstan-ignore-next-line |
| 15 | $message = $notification->toJPush($notifiable); | 13 | $notification->toJPush($notifiable); |
| 16 | |||
| 17 | JPushMessageJob::dispatchSync( | ||
| 18 | Arr::get($message, 'user', []), | ||
| 19 | Arr::get($message, 'title', ''), | ||
| 20 | Arr::get($message, 'content', ''), | ||
| 21 | Arr::get($message, 'data', []) | ||
| 22 | ); | ||
| 23 | } | 14 | } |
| 24 | } | 15 | } | ... | ... |
app/Jobs/JPushMessageJob.php
deleted
100644 → 0
| 1 | <?php | ||
| 2 | |||
| 3 | namespace App\Jobs; | ||
| 4 | |||
| 5 | use Arr; | ||
| 6 | use Illuminate\Bus\Queueable; | ||
| 7 | use Illuminate\Contracts\Queue\ShouldQueue; | ||
| 8 | use Illuminate\Foundation\Bus\Dispatchable; | ||
| 9 | use Illuminate\Queue\InteractsWithQueue; | ||
| 10 | use Illuminate\Queue\SerializesModels; | ||
| 11 | use JPush\Client; | ||
| 12 | use JPush\Exceptions\APIRequestException; | ||
| 13 | use JPush\Exceptions\ServiceNotAvaliable; | ||
| 14 | use Log; | ||
| 15 | |||
| 16 | |||
| 17 | class JPushMessageJob implements ShouldQueue | ||
| 18 | { | ||
| 19 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @var \JPush\Client | ||
| 23 | */ | ||
| 24 | protected Client $client; | ||
| 25 | |||
| 26 | protected array $user; | ||
| 27 | |||
| 28 | protected string $title; | ||
| 29 | |||
| 30 | protected string $content; | ||
| 31 | |||
| 32 | protected array $extras; | ||
| 33 | |||
| 34 | protected array $config; | ||
| 35 | |||
| 36 | |||
| 37 | /** | ||
| 38 | * Create a new job instance. | ||
| 39 | * @param array<int,string>|string $user | ||
| 40 | * @param string $title | ||
| 41 | * @param string $content | ||
| 42 | * @param array<string,mixed> $extras | ||
| 43 | */ | ||
| 44 | public function __construct(array|string $user, string $title, string $content, array $extras = []) | ||
| 45 | { | ||
| 46 | $this->config = config('services.jpush'); | ||
| 47 | |||
| 48 | $this->client = new Client($this->getConfig('key'), $this->getConfig('secret'), NULL); | ||
| 49 | |||
| 50 | $this->user = Arr::map(Arr::wrap($user), fn($item) => $this->getConfig('prefix') . $item); | ||
| 51 | |||
| 52 | [$this->title, $this->content, $this->extras] = [$title, $content, $extras]; | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Execute the job. | ||
| 57 | * | ||
| 58 | * @return void | ||
| 59 | * @throws \Random\RandomException | ||
| 60 | */ | ||
| 61 | public function handle(): void | ||
| 62 | { | ||
| 63 | try { | ||
| 64 | $response = $this->send(); | ||
| 65 | } catch (APIRequestException|ServiceNotAvaliable $exception) { | ||
| 66 | $response = ['http_code' => $exception->getHttpCode(), 'body' => $exception->getMessage()]; | ||
| 67 | } | ||
| 68 | |||
| 69 | Log::channel('jpush')->info('JPush Message Status[' . $response['http_code'] . ']: ' . PHP_EOL, [ | ||
| 70 | 'user' => $this->user, | ||
| 71 | 'request' => ['title' => $this->title, 'content' => $this->content, 'extras' => $this->extras], | ||
| 72 | 'response' => $response['body'] | ||
| 73 | ]); | ||
| 74 | } | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @param string $key | ||
| 78 | * @return mixed | ||
| 79 | */ | ||
| 80 | private function getConfig(string $key): mixed | ||
| 81 | { | ||
| 82 | return Arr::get($this->config, $key); | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * @return array | ||
| 87 | * @throws \Random\RandomException | ||
| 88 | */ | ||
| 89 | private function send(): array | ||
| 90 | { | ||
| 91 | return $this->client->push()->setPlatform('all')->addAlias($this->user) | ||
| 92 | ->iosNotification(['title' => $this->title, 'body' => $this->content], ['extras' => $this->extras]) | ||
| 93 | ->androidNotification($this->content, ['title' => $this->title, 'alert_type' => 7, 'extras' => $this->extras]) | ||
| 94 | ->options([ | ||
| 95 | 'apns_production' => $this->getConfig('apns_production'), | ||
| 96 | 'time_to_live' => 86400 * 3, | ||
| 97 | 'sendno' => random_int(100, 99999999), | ||
| 98 | 'third_party_channel' => [ | ||
| 99 | 'xiaomi' => ['importance' => 'HIGH'], | ||
| 100 | 'huawei' => ['importance' => 'HIGH'], | ||
| 101 | 'honor' => ['importance' => 'NORMAL'], | ||
| 102 | 'meizu' => ['importance' => 'HIGH'], | ||
| 103 | 'oppo' => ['importance' => 'HIGH'], | ||
| 104 | 'vivo' => ['importance' => 'HIGH'], | ||
| 105 | ] | ||
| 106 | ]) | ||
| 107 | ->send(); | ||
| 108 | } | ||
| 109 | } |
-
Please register or sign in to post a comment