/var/www/vhosts/nabawater/common/helpers
Edit: /var/www/vhosts/nabawater/common/helpers/OneSignal.php (7377B)
*/
class OneSignal
{
/**
* One signal API URL
*/
const URL = 'https://onesignal.com/api/v1/notifications';
/**
* @var static
*/
private static $instance;
/**
* @var string
*/
private $appId;
/**
* @var string
*/
private $restApiKey;
/**
* @var string
*/
private $notify_icon = '';
/**
* OneSignal constructor.
*/
public function __construct()
{
$this->setAppId(Configuration::get(Configuration::PUSH_NOTIFICATION_APP_ID));
$this->setRestApiKey(Configuration::get(Configuration::PUSH_NOTIFICATION_REST_API_KEY));
}
/**
* @return static
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param mixed $restApiKey
*/
public function setRestApiKey($restApiKey)
{
$this->restApiKey = $restApiKey;
}
/**
* @param string $appId
*/
public function setAppId($appId)
{
$this->appId = $appId;
}
/**
* @param $icon
* @return $this
*/
public function setIcon($icon)
{
$this->notify_icon = $icon;
return $this;
}
/**
* @param array $title
* @param array $message
* @param array $clientIds
* @param array $data
* @return mixed
* @throws \yii\base\InvalidParamException
*/
public function push(array $title, array $message, array $clientIds, array $data)
{
$fields = [
'app_id' => $this->appId,
'contents' => $message,
'headings' => $title,
'large_icon' => $this->notify_icon,
'small_icon' => $this->notify_icon,
];
if (Configuration::get(Configuration::PUSH_NOTIFICATION_CHANNEL_ID) != '') {
$fields['android_channel_id'] = Configuration::get(Configuration::PUSH_NOTIFICATION_CHANNEL_ID);
$fields['ios_sound'] = 'notification.wav';
$fields['android_sound'] = 'notification';
}
if ($data !== []) {
$fields['data'] = $data;
}
if ($clientIds === []) {
$fields['included_segments'] = 'All Users';
} else {
$fields['include_player_ids'] = $clientIds;
}
$fields = Json::encode($fields);
$headers = [
'Content-Type: application/json; charset=utf-8',
sprintf('Authorization: Basic %s', $this->restApiKey)
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, static::URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function vendorpush(array $title, array $message, array $clientIds, array $data)
{
$fields = [
'app_id' => $this->appId,
'contents' => $message,
'headings' => $title,
'large_icon' => $this->notify_icon,
'small_icon' => $this->notify_icon,
];
if (Configuration::get(Configuration::VENDOR_PUSH_NOTIFICATION_CHANNEL_ID) != '') {
$fields['android_channel_id'] = Configuration::get(Configuration::VENDOR_NOTIFICATION_CHANNEL_ID);
$fields['ios_sound'] = 'notification.wav';
$fields['android_sound'] = 'notification';
}
if ($data !== []) {
$fields['data'] = $data;
}
if ($clientIds === []) {
$fields['included_segments'] = 'All Users';
} else {
$fields['include_player_ids'] = $clientIds;
}
$fields = Json::encode($fields);
$headers = [
'Content-Type: application/json; charset=utf-8',
sprintf('Authorization: Basic %s', $this->restApiKey)
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, static::URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function sendCustomerNotification(array $title, array $message, $device_type = 3, $user_type = '')
{
$isIos = True;
$isAndroid = True;
if ($device_type == 1) {
$isIos = False;
}
if ($device_type == 2) {
$isAndroid = False;
}
$fields = [
'app_id' => $this->appId,
'contents' => $message,
'headings' => $title,
];
// Add icon if set
if (!empty($this->notify_icon)) {
$fields['large_icon'] = $this->notify_icon;
$fields['small_icon'] = $this->notify_icon;
}
if (Configuration::get(Configuration::PUSH_NOTIFICATION_CHANNEL_ID) != '') {
$fields['android_channel_id'] = Configuration::get(Configuration::PUSH_NOTIFICATION_CHANNEL_ID);
$fields['ios_sound'] = 'notification.wav';
$fields['android_sound'] = 'notification';
}
// Filter by device type
if ($device_type == 1) {
// Android only
$fields['filters'] = [['field' => 'device_type', 'value' => 1]];
} elseif ($device_type == 2) {
// iOS only
$fields['filters'] = [['field' => 'device_type', 'value' => 0]];
}
$fields['included_segments'] = ['All'];
$headers = [
'Content-Type: application/json; charset=utf-8',
sprintf('Authorization: Basic %s', $this->restApiKey)
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, static::URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, Json::encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$curlError = curl_error($ch);
curl_close($ch);
// Log only errors
if ($curlError) {
$this->log('cURL Error: ' . $curlError);
}
return $response;
}
/**
* @param $msg
* @throws \yii\base\InvalidParamException
*/
private function log($msg)
{
Com::log($msg, 'one-signal');
}
}