1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Fcm; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use Kreait\Firebase\Exception\MessagingException; |
8
|
|
|
use Kreait\Firebase\Messaging\Message; |
9
|
|
|
use Kreait\Laravel\Firebase\Facades\FirebaseMessaging; |
10
|
|
|
use NotificationChannels\Fcm\Exceptions\CouldNotSendNotification; |
11
|
|
|
|
12
|
|
|
class FcmChannel |
13
|
|
|
{ |
14
|
|
|
const MAX_TOKEN_PER_REQUEST = 500; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Send the given notification. |
23
|
|
|
* |
24
|
|
|
* @param mixed $notifiable |
25
|
|
|
* @param Notification $notification |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
* @throws CouldNotSendNotification |
29
|
|
|
*/ |
30
|
|
|
public function send($notifiable, Notification $notification) |
31
|
|
|
{ |
32
|
|
|
$tokens = (array) $notifiable->routeNotificationFor('fcm'); |
33
|
|
|
|
34
|
|
|
if (empty($tokens)) { |
35
|
|
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Get the message from the notification class |
39
|
|
|
$fcmMessage = $notification->toFcm($notifiable); |
|
|
|
|
40
|
|
|
|
41
|
|
|
if (! $fcmMessage instanceof Message) { |
42
|
|
|
throw new CouldNotSendNotification('The toFcm() method only accepts instances of ' . Message::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$responses = []; |
46
|
|
|
|
47
|
|
|
// Use multicast because there are multiple recipients |
48
|
|
|
$partialTokens = array_chunk($tokens, self::MAX_TOKEN_PER_REQUEST, false); |
49
|
|
|
foreach ($partialTokens as $tokens) { |
50
|
|
|
$responses[] = $this->sendToFcmMulticast($fcmMessage, $tokens); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $responses; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $fcmMessage |
58
|
|
|
* @param $tokens |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
* @throws CouldNotSendNotification |
62
|
|
|
*/ |
63
|
|
|
protected function sendToFcmMulticast($fcmMessage, $tokens) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
return FirebaseMessaging::sendMulticast($fcmMessage, $tokens); |
67
|
|
|
} catch (MessagingException $messagingException) { |
68
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($messagingException); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.