Skip to content

Commit

Permalink
accepts also string tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
alchalade committed Feb 15, 2021
1 parent acf752d commit d8c4e9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Exceptions/UnsupportedTokenFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Kutia\Larafirebase\Exceptions;


class UnsupportedTokenFormat extends \Exception
{

}
20 changes: 16 additions & 4 deletions src/Services/Larafirebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kutia\Larafirebase\Services;

use Illuminate\Support\Facades\Http;
use Kutia\Larafirebase\Exceptions\UnsupportedTokenFormat;

class Larafirebase
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public function fromArray($fromArray)
public function sendNotification($tokens)
{
$fields = array(
'registration_ids' => $tokens,
'registration_ids' => $this->validateToken($tokens),
'notification' => ($this->fromArray) ? $this->fromArray : [
'title' => $this->title,
'body' => $this->body,
Expand All @@ -76,14 +77,14 @@ public function sendNotification($tokens)
],
'priority' => $this->priority
);

return $this->callApi($fields);
}

public function sendMessage($tokens)
{
$fields = array(
'registration_ids' => $tokens,
'registration_ids' => $this->validateToken($tokens),
'data' => ($this->fromArray) ? $this->fromArray : [
'title' => $this->title,
'body' => $this->body,
Expand All @@ -98,9 +99,20 @@ public function sendMessage($tokens)
public function callApi($fields)
{
$response = Http::withHeaders([
'Authorization' => 'key='. config('larafirebase.authentication_key')
'Authorization' => 'key=' . config('larafirebase.authentication_key')
])->post(self::API_URI, $fields);

return $response->body();
}

private function validateToken($tokens)
{
if (is_array($tokens)) {
return $tokens;
}
if (is_string($tokens)) {
return explode(',', $tokens);
}
throw new UnsupportedTokenFormat('Please pass tokens as array [token1, token2] or as string (use comma as separator if multiple passed)');
}
}

0 comments on commit d8c4e9f

Please sign in to comment.