Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…irebase into 1.x
  • Loading branch information
gentritabazi committed Feb 21, 2021
2 parents 6840422 + 42e5660 commit f407826
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ return Larafirebase::fromRaw([

### Author
* Name: **Gentrit Abazi**
* Email: **gentritabazi01@gmail.com**
* Email: **gentritabazi@kutia.net**
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;

use Exception;

class UnsupportedTokenFormat extends Exception
{
//
}
22 changes: 18 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 @@ -76,7 +77,7 @@ public function fromRaw($fromRaw)
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 @@ -85,14 +86,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 @@ -112,9 +113,22 @@ public function send()
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 f407826

Please sign in to comment.