LaravelFCM\Sender\FCMSender::sendTo PHP Method

sendTo() public method

- a unique device with is registration Token - or to multiples devices with an array of registrationIds
public sendTo ( String | array $to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null ) : DownstreamResponse | null
$to String | array
$options LaravelFCM\Message\Options
$notification LaravelFCM\Message\PayloadNotification
$data LaravelFCM\Message\PayloadData
return LaravelFCM\Response\DownstreamResponse | null
    public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
    {
        $response = null;
        if (is_array($to) && !empty($to)) {
            $partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
            foreach ($partialTokens as $tokens) {
                $request = new Request($tokens, $options, $notification, $data);
                $responseGuzzle = $this->post($request);
                $responsePartial = new DownstreamResponse($responseGuzzle, $tokens);
                if (!$response) {
                    $response = $responsePartial;
                } else {
                    $response->merge($responsePartial);
                }
            }
        } else {
            $request = new Request($to, $options, $notification, $data);
            $responseGuzzle = $this->post($request);
            $response = new DownstreamResponse($responseGuzzle, $to);
        }
        return $response;
    }

Usage Example

Beispiel #1
0
    /**
     * @test
     */
    public function an_empty_array_of_tokens_thrown_an_exception()
    {
        $response = new Response(400, [], '{ 
						  "multicast_id": 216,
						  "success": 3,
						  "failure": 3,
						  "canonical_ids": 1,
						  "results": [
							    { "message_id": "1:0408" },
							    { "error": "Unavailable" },
							    { "error": "InvalidRegistration" },
							    { "message_id": "1:1516" },
							    { "message_id": "1:2342", "registration_id": "32" },
							    { "error": "NotRegistered"}
	                      ]
					}');
        $client = Mockery::mock(Client::class);
        $client->shouldReceive('post')->once()->andReturn($response);
        $fcm = new FCMSender($client, 'http://test.test');
        $this->setExpectedException(\LaravelFCM\Response\Exceptions\InvalidRequestException::class);
        $fcm->sendTo([]);
    }