Skip to content

Instantly share code, notes, and snippets.

@pravodev
Last active December 6, 2018 12:51
Show Gist options
  • Save pravodev/3b59d8ef4bfd50190ca02f53faa27193 to your computer and use it in GitHub Desktop.
Save pravodev/3b59d8ef4bfd50190ca02f53faa27193 to your computer and use it in GitHub Desktop.
FCM Classes
<?php
/**
* Push Notification use FCM
*
* @author Rifqi Khoeruman Azam
*/
namespace App\Libraries;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use FCM;
class PushNotification
{
/**
* Title untuk notfikasi nya
*
* @var string
*/
protected $title;
/**
* Isi dari notifikasi
*
* @var string $body
*/
protected $body;
/**
* Tujuan / token device
*
* @var string|array
*/
protected $to;
/**
* Redirect To Screen in mobile apps
*
* @var string
*/
protected $ccreen;
public function __construct($title, $body)
{
$this->title = $title;
$this->body = $body;
}
/**
* Set property to
*
* @return $this
*/
public function to($to)
{
$this->to = $to;
return $this;
}
/**
* Redirect to screen in mobile apps
*
* @param string $screen
*/
public function redirectTo($screen)
{
$this->screen = $screen;
return $this;
}
/**
* Send Notification
*
* @return $this
*/
public function send()
{
$optionBuilder = new OptionsBuilder();
$optionBuilder->setTimeToLive(60*20);
$notificationBuilder = new PayloadNotificationBuilder($this->title);
$notificationBuilder->setBody($this->body)
->setSound('default');
$dataBuilder = new PayloadDataBuilder();
if($this->screen){
$dataBuilder->addData(['screen' => $this->screen]);
}
// $dataBuilder->addData(['a_data' => 'my_data']);
$option = $optionBuilder->build();
$notification = $notificationBuilder->build();
$data = $dataBuilder->build();
$downstreamResponse = FCM::sendTo($this->to, $option, $notification, $data);
if(count($downstreamResponse->tokensToRetry())){
$this->to($downstreamResponse->tokensToRetry());
$this->send();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment