|
<?php |
|
|
|
/** |
|
* Created by PhpStorm. |
|
* User: hafiq |
|
* Date: 18/03/2019 |
|
* Time: 11:59 PM. |
|
*/ |
|
|
|
namespace App\Firebase; |
|
|
|
use App\Exceptions\FirebaseNotificationException; |
|
|
|
class FireBaseNotification |
|
{ |
|
protected $headers = []; |
|
protected $key = null; |
|
protected $baseUrl = null; |
|
protected $pushBy = 1; // 1- topic 2- device |
|
protected $payload = null; |
|
protected $channelRegistrationIds = null; |
|
protected $channelTopic = null; |
|
protected $to_whom = null; |
|
protected $priority = 'high'; |
|
protected $env = 'production'; |
|
|
|
public function __construct() |
|
{ |
|
$this->key = config('notification.fire_key'); |
|
|
|
$this->headers = [ |
|
'Content-Type' => 'application/json', |
|
'Authorization' => 'key='.$this->key, |
|
]; |
|
|
|
$this->baseUrl = config('notification.fire_url'); |
|
} |
|
|
|
public function setHeaders(array $headers): self |
|
{ |
|
if ($headers) { |
|
$this->headers = array_merge($this->headers, $headers); |
|
} |
|
|
|
return $this; |
|
} |
|
|
|
public function setKey(string $key): self |
|
{ |
|
if ($key) { |
|
$this->key = $key; |
|
$this->headers['Authorization'] = 'key='.$this->key; |
|
} else { |
|
throw new FirebaseNotificationException('Firebase Server Key is needed. It Cannot be null!!'); |
|
} |
|
|
|
return $this; |
|
} |
|
|
|
public function setBaseUrl(string $baseUrl): self |
|
{ |
|
if ($baseUrl) { |
|
$this->baseUrl = $baseUrl; |
|
} else { |
|
throw new FirebaseNotificationException('Firebase URL is needed. It Cannot be null!!'); |
|
} |
|
|
|
return $this; |
|
} |
|
|
|
public function pushByTopic() |
|
{ |
|
$this->pushBy = 1; |
|
return $this; |
|
} |
|
|
|
public function pushByDevice() |
|
{ |
|
$this->pushBy = 2; |
|
return $this; |
|
} |
|
|
|
public function setPayload(array $payload): self |
|
{ |
|
$this->payload = $payload; |
|
|
|
return $this; |
|
} |
|
|
|
public function setPriority(string $priority): self |
|
{ |
|
$this->priority = $priority; |
|
|
|
return $this; |
|
} |
|
|
|
public function onlyForDevelopment(): self |
|
{ |
|
$this->env = 'local'; |
|
|
|
return $this; |
|
} |
|
|
|
public function setRegistrationIds($channelName): self |
|
{ |
|
$this->channelRegistrationIds = $channelName; |
|
|
|
return $this; |
|
} |
|
|
|
public function setTopic($channelTopic) |
|
{ |
|
$this->channelTopic = $channelTopic; |
|
|
|
return $this; |
|
} |
|
|
|
public function sendTo(string $to_whom): self |
|
{ |
|
$this->to_whom = preg_replace('/\s+/', '_', strtolower($to_whom)); |
|
|
|
return $this; |
|
} |
|
|
|
public function push() |
|
{ |
|
$response = api_request() |
|
->baseUrl($this->baseUrl) |
|
->postMethod() |
|
->setHeader($this->headers) |
|
->jsonRequest() |
|
->setRequestBody($this->getBody()) |
|
->setParam([ |
|
'verify' => false, |
|
])->fetch(); |
|
|
|
if (! $response['error']) { |
|
return [ |
|
'data' => 'Notification Success', |
|
'statusCode' => $response['status_code'], |
|
'payload' => $response, |
|
]; |
|
} else { |
|
return $response; |
|
} |
|
} |
|
|
|
private function getBody() |
|
{ |
|
if ($this->pushBy == 1) { |
|
if (!$this->channelTopic) { |
|
throw new FirebaseNotificationException("Topic is Required"); |
|
} |
|
|
|
$this->channelTopic = "/topics/{$this->channelTopic}"; |
|
|
|
if ($this->to_whom) { |
|
$this->channelTopic .= "-{$this->to_whom}"; |
|
} |
|
|
|
if ($this->env == 'local') { |
|
$this->channelTopic .= '-local'; |
|
} |
|
|
|
if ($this->to_whom == 'android') { |
|
return $this->buildBodyAndroid(); |
|
} elseif ($this->to_whom == 'ios') { |
|
return $this->buildBodyIOS(); |
|
} else { |
|
return $this->buildBodyUniversal(); |
|
} |
|
} else { |
|
if (!$this->channelRegistrationIds) { |
|
throw new FirebaseNotificationException("Push Token is Required"); |
|
} |
|
|
|
$this->channelRegistrationIds = is_array($this->channelRegistrationIds) ? $this->channelRegistrationIds : [$this->channelRegistrationIds]; |
|
return $this->buildBodyUniversal(); |
|
} |
|
} |
|
|
|
private function buildBodyUniversal() |
|
{ |
|
$this->payload['sound'] = 'default'; |
|
$data = [ |
|
'priority' => $this->priority, |
|
'data' => $this->payload, |
|
'notification' => $this->payload, |
|
'content_available' => true, |
|
]; |
|
|
|
if ($this->pushBy == 1) { |
|
$data['to'] = $this->channelTopic; |
|
} else { |
|
$data['registration_ids'] = $this->channelRegistrationIds; |
|
} |
|
|
|
return $data; |
|
} |
|
|
|
private function buildBodyAndroid() |
|
{ |
|
$data = [ |
|
'priority' => $this->priority, |
|
'data' => $this->payload, |
|
]; |
|
|
|
if ($this->pushBy = 1) { |
|
$data['to'] = $this->channelTopic; |
|
} else { |
|
$data['registration_ids'] = $this->channelRegistrationIds; |
|
} |
|
|
|
return $data; |
|
} |
|
|
|
private function buildBodyIOS() |
|
{ |
|
$this->payload['sound'] = 'default'; |
|
|
|
$data = [ |
|
'priority' => $this->priority, |
|
'notification' => $this->payload, |
|
'content_available' => true, |
|
]; |
|
|
|
if ($this->pushBy = 1) { |
|
$data['to'] = $this->channelTopic; |
|
} else { |
|
$data['registration_ids'] = $this->channelRegistrationIds; |
|
} |
|
|
|
return $data; |
|
} |
|
} |