|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the FirebaseCloudMessagingBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Artem Henvald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Fresh\FirebaseCloudMessagingBundle\Command; |
|
14
|
|
|
|
|
15
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Client\FirebaseClient; |
|
16
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\MessageFactory; |
|
17
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Options\OptionsFactory; |
|
18
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Options\Priority; |
|
19
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Payload\PayloadFactory; |
|
20
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Target\TargetFactory; |
|
21
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MulticastMessageResponseInterface; |
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
26
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class FirebaseSendTestMessageCommand. |
|
30
|
|
|
*/ |
|
31
|
|
|
class FirebaseSendTestMessageCommand extends Command |
|
32
|
|
|
{ |
|
33
|
|
|
private FirebaseClient $firebaseClient; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
private SymfonyStyle $io; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param FirebaseClient $firebaseClient |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(FirebaseClient $firebaseClient) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct(); |
|
43
|
|
|
$this->firebaseClient = $firebaseClient; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function configure(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this |
|
52
|
|
|
->setName('firebase:send:test-message') |
|
53
|
|
|
->setDescription('Sends test message with the "notification" payload to the specific registration token') |
|
54
|
|
|
->setHelp( |
|
55
|
|
|
<<<'HELP' |
|
56
|
|
|
The <info>%command.name%</info> sends test message with the "notification" payload to the specific registration token. |
|
57
|
|
|
|
|
58
|
|
|
<info>%command.full_name% registration_token</info> |
|
59
|
|
|
HELP |
|
60
|
|
|
) |
|
61
|
|
|
->addArgument('registration_token', InputArgument::REQUIRED, 'Registration token of a recipient'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->io = new SymfonyStyle($input, $output); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->io->title('Sending message...'); |
|
78
|
|
|
|
|
79
|
|
|
$message = MessageFactory::createAndroidMessage() |
|
80
|
|
|
->setTarget( |
|
81
|
|
|
TargetFactory::createSingleRecipientTarget() |
|
82
|
|
|
->setRegistrationToken($input->getArgument('registration_token')) |
|
83
|
|
|
) |
|
84
|
|
|
->setOptions( |
|
85
|
|
|
OptionsFactory::createOptions() |
|
86
|
|
|
->setPriority(Priority::NORMAL) |
|
87
|
|
|
->setCollapseKey('test') |
|
88
|
|
|
) |
|
89
|
|
|
->setPayload( |
|
90
|
|
|
PayloadFactory::createNotificationAndroidPayload() |
|
91
|
|
|
->setTitle('Hello world!') |
|
92
|
|
|
->setBody('It is a test message, ignore it.') |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$response = $this->firebaseClient->sendMessage($message); |
|
96
|
|
|
|
|
97
|
|
|
if ($response instanceof MulticastMessageResponseInterface) { |
|
98
|
|
|
$this->showResponseDetails($response); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->io->success('Done'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param MulticastMessageResponseInterface $response |
|
106
|
|
|
*/ |
|
107
|
|
|
private function showResponseDetails(MulticastMessageResponseInterface $response): void |
|
108
|
|
|
{ |
|
109
|
|
|
if ($response->hasSuccessfulMessageResults()) { |
|
110
|
|
|
$this->io->title('Successfully sent messages'); |
|
111
|
|
|
|
|
112
|
|
|
$headers = ['Registration Token', 'Message Id']; |
|
113
|
|
|
$rows = []; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($response->getSuccessfulMessageResults() as $messageResult) { |
|
116
|
|
|
$rows[] = [ |
|
117
|
|
|
$this->shortinizeToken($messageResult->getToken()), |
|
118
|
|
|
$messageResult->getMessageId(), |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->io->table($headers, $rows); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// if ($response->hasCanonicalTokenMessageResults()) { |
|
126
|
|
|
// foreach ($response->getCanonicalTokenMessageResults() as $messageResult) { |
|
127
|
|
|
// $rows[] = [ |
|
128
|
|
|
// $messageResult->getToken(), |
|
129
|
|
|
// $messageResult->getMessageId(), |
|
130
|
|
|
// $messageResult->getCanonicalToken(), |
|
131
|
|
|
// null, |
|
132
|
|
|
// ]; |
|
133
|
|
|
// } |
|
134
|
|
|
// } |
|
135
|
|
|
// |
|
136
|
|
|
// if ($response->hasFailedMessageResults()) { |
|
137
|
|
|
// foreach ($response->getFailedMessageResults() as $messageResult) { |
|
138
|
|
|
// $rows[] = [ |
|
139
|
|
|
// $messageResult->getToken(), |
|
140
|
|
|
// null, |
|
141
|
|
|
// null, |
|
142
|
|
|
// $messageResult->getError(), |
|
143
|
|
|
// ]; |
|
144
|
|
|
// } |
|
145
|
|
|
// } |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $token |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
|
|
private function shortinizeToken(string $token): string |
|
154
|
|
|
{ |
|
155
|
|
|
$positionOfColon = \strpos($token, ':'); |
|
156
|
|
|
|
|
157
|
|
|
return \substr($token, 0, $positionOfColon).\str_pad('', $positionOfColon, '.').\substr($token, -$positionOfColon, $positionOfColon); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|