1 | <?php |
||
16 | class FcmChannel |
||
17 | { |
||
18 | const MAX_TOKEN_PER_REQUEST = 500; |
||
19 | |||
20 | /** |
||
21 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
22 | */ |
||
23 | protected $events; |
||
24 | |||
25 | /** |
||
26 | * FcmChannel constructor. |
||
27 | * |
||
28 | * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher |
||
29 | */ |
||
30 | public function __construct(Dispatcher $dispatcher) |
||
34 | |||
35 | /** |
||
36 | * @var string|null |
||
37 | */ |
||
38 | protected $fcmProject = null; |
||
39 | |||
40 | /** |
||
41 | * Send the given notification. |
||
42 | * |
||
43 | * @param mixed $notifiable |
||
44 | * @param \Illuminate\Notifications\Notification $notification |
||
45 | * |
||
46 | * @return array |
||
47 | * @throws \NotificationChannels\Fcm\Exceptions\CouldNotSendNotification |
||
48 | * @throws \Kreait\Firebase\Exception\FirebaseException |
||
49 | */ |
||
50 | public function send($notifiable, Notification $notification) |
||
51 | { |
||
52 | $token = $notifiable->routeNotificationFor('fcm', $notification); |
||
53 | |||
54 | // Get the message from the notification class |
||
55 | $fcmMessage = $notification->toFcm($notifiable); |
||
|
|||
56 | |||
57 | if (! $fcmMessage instanceof Message) { |
||
58 | throw CouldNotSendNotification::invalidMessage(); |
||
59 | } |
||
60 | |||
61 | $this->fcmProject = null; |
||
62 | if (method_exists($notification, 'fcmProject')) { |
||
63 | $this->fcmProject = $notification->fcmProject($notifiable, $fcmMessage); |
||
64 | } |
||
65 | |||
66 | $responses = []; |
||
67 | |||
68 | try { |
||
69 | if (empty($token) && $fcmMessage instanceof FcmMessage) { |
||
70 | if ($fcmMessage->getTopic() || $fcmMessage->getCondition()) { |
||
71 | $responses[] = $this->sendToFcm($fcmMessage); |
||
72 | } |
||
73 | } elseif (empty($token) && $fcmMessage instanceof CloudMessage) { |
||
74 | if ($fcmMessage->hasTarget()) { |
||
75 | $responses[] = $this->sendToFcm($fcmMessage); |
||
76 | } |
||
77 | } elseif (empty($token)) { |
||
78 | $responses = []; |
||
79 | } elseif (! is_array($token)) { |
||
80 | if ($fcmMessage instanceof CloudMessage) { |
||
81 | $fcmMessage = $fcmMessage->withChangedTarget('token', $token); |
||
82 | } |
||
83 | |||
84 | if ($fcmMessage instanceof FcmMessage) { |
||
85 | $fcmMessage->setToken($token); |
||
86 | } |
||
87 | |||
88 | $responses[] = $this->sendToFcm($fcmMessage); |
||
89 | } else { |
||
90 | // Use multicast because there are multiple recipients |
||
91 | $partialTokens = array_chunk($token, self::MAX_TOKEN_PER_REQUEST, false); |
||
92 | foreach ($partialTokens as $tokens) { |
||
93 | $responses[] = $this->sendToFcmMulticast($fcmMessage, $tokens); |
||
94 | } |
||
95 | } |
||
96 | } catch (MessagingException $exception) { |
||
97 | $this->failedNotification($notifiable, $notification, $exception); |
||
98 | throw CouldNotSendNotification::serviceRespondedWithAnError($exception); |
||
99 | } |
||
100 | |||
101 | return $responses; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return \Kreait\Firebase\Messaging |
||
106 | */ |
||
107 | protected function messaging() |
||
119 | |||
120 | /** |
||
121 | * @param \Kreait\Firebase\Messaging\Message $fcmMessage |
||
122 | * @return array |
||
123 | * @throws \Kreait\Firebase\Exception\MessagingException |
||
124 | * @throws \Kreait\Firebase\Exception\FirebaseException |
||
125 | */ |
||
126 | protected function sendToFcm(Message $fcmMessage) |
||
130 | |||
131 | /** |
||
132 | * @param $fcmMessage |
||
133 | * @param array $tokens |
||
134 | * @return \Kreait\Firebase\Messaging\MulticastSendReport |
||
135 | * @throws \Kreait\Firebase\Exception\MessagingException |
||
136 | * @throws \Kreait\Firebase\Exception\FirebaseException |
||
137 | */ |
||
138 | protected function sendToFcmMulticast($fcmMessage, array $tokens) |
||
142 | |||
143 | /** |
||
144 | * Dispatch failed event. |
||
145 | * |
||
146 | * @param mixed $notifiable |
||
147 | * @param \Illuminate\Notifications\Notification $notification |
||
148 | * @param \Throwable $exception |
||
149 | * @return array|null |
||
150 | */ |
||
151 | protected function failedNotification($notifiable, Notification $notification, Throwable $exception) |
||
163 | } |
||
164 |
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.