1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Apn\Console; |
4
|
|
|
|
5
|
|
|
use Notification; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use NotificationChannels\Apn\Notifications\ApnTestNotification; |
8
|
|
|
|
9
|
|
|
class ApnSendTestNotification extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'apn:test |
17
|
|
|
{--T|token= : The token of the device that the PN will be sent to.} |
18
|
|
|
{--t|title=Test APN Notification : The title of the PN.} |
19
|
|
|
{--m|message=This is a test push notification sent from the console. : The message body for the PN.} |
20
|
|
|
{--b|badge=1 : The number that will be displayed in the app\'s badge notification.} |
21
|
|
|
{--s|sound=default : The name of the sound that should be played with the notification.}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Send a test push notification using the APN service.'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute the console command. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function handle(): void |
36
|
|
|
{ |
37
|
|
|
if ($errors = $this->optionsAreInvalid()) { |
38
|
|
|
foreach ($errors as $error) { |
39
|
|
|
$this->error($error); |
40
|
|
|
} |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->sendNotification(); |
45
|
|
|
$this->info('Push notification sent.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Validate whether if the options passed in the command |
50
|
|
|
* are valid. An array of errors is returned if any |
51
|
|
|
* options fail the checks. Null is returned if |
52
|
|
|
* the options are all valid. |
53
|
|
|
* |
54
|
|
|
* @param array $errors |
55
|
|
|
* @return array|null |
56
|
|
|
*/ |
57
|
|
|
private function optionsAreInvalid(array $errors = []) |
58
|
|
|
{ |
59
|
|
|
if (!$this->option('token')) { |
60
|
|
|
$errors[] = 'No device token (--token) is specified.'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->option('badge') != '0' && !(int)$this->option('badge')) { |
64
|
|
|
$errors[] = 'The badge number (--badge) must be an integer.'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $errors ?? null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Trigger the notification to send to the testing device. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
private function sendNotification() |
76
|
|
|
{ |
77
|
|
|
Notification::route('apn', $this->option('token')) |
78
|
|
|
->notify(new APNTestNotification($this->option('title'), $this->option('message'), (int)$this->option('badge'), $this->option('sound'))); |
79
|
|
|
} |
80
|
|
|
} |