Skip to content

Instantly share code, notes, and snippets.

@nguyenphuocnhatthanh
Created June 14, 2018 07:20
Show Gist options
  • Save nguyenphuocnhatthanh/7e39c5365aed116aa448a15d5b2fb84a to your computer and use it in GitHub Desktop.
Save nguyenphuocnhatthanh/7e39c5365aed116aa448a15d5b2fb84a to your computer and use it in GitHub Desktop.
Abstact Notification Firebase
<?php
namespace App\Services\PushNotification;
use App\Services\DeviceToken;
use App\Services\FirebaseCloudMessage;
abstract class AbstractNotification
{
/**
* @var FirebaseCloudMessage
*/
private $firebase;
/**
* CommentPost constructor.
* @param FirebaseCloudMessage $firebaseCloudMessage
*/
public function __construct(FirebaseCloudMessage $firebaseCloudMessage)
{
$this->firebase = $firebaseCloudMessage;
}
/**
* Push notification for Android Platform
*
* @param $tokens
* @param array $params
*/
protected function pushDeviceAndroid($tokens, array $params)
{
/* @Object FireBaseCloudMessage */
$this->setMessage($tokens, $params)->sendMessage();
}
/**
* Push notification for IOS Platform
*
* @param $tokens
* @param array $params
*/
protected function pushDeviceIOS($tokens, array $params)
{
/* @Object FireBaseCloudMessage */
$this->setMessage($tokens, $params)->sendMessageIOS();
}
/**
* Push notification for multi platform
*
* @param DeviceToken $deviceToken
* @param array $params
*/
protected function bulkPush($deviceToken, array $params)
{
if ($tokens = $deviceToken->getTokenIOS()) {
$this->pushDeviceIOS($tokens, $params);
}
if ($tokens = $deviceToken->getTokenAndroid()) {
$this->pushDeviceAndroid($tokens, $params);
}
}
/**
* Set device token
*
* @param array $devices
* @return DeviceToken
*/
protected function setDeviceToken(array $devices)
{
$device = new DeviceToken($devices);
return $device->setTokenPlatform();
}
/**
* @param $tokens
* @param array $params
* @internal param $notification
* @return FirebaseCloudMessage
*/
protected function setMessage($tokens, array $params)
{
$firebase = $this->firebase->setRegistrationIds($tokens)
->setNotification($params['notification']);
return $firebase;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment