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