51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Models\Setting;
|
|
|
|
trait OneSignalHelper
|
|
{
|
|
public function setupEnvironment(): array
|
|
{
|
|
$setting = Setting::first();
|
|
$appKey = $setting->onesignal_app_id;
|
|
$secretKey = $setting->one_signal_rest_api_key;;
|
|
return [
|
|
'appKey' => $appKey,
|
|
'secretKey' => $secretKey
|
|
];
|
|
}
|
|
|
|
|
|
public function sendAppNotification(string $notificationTitle, string $notificationContent)
|
|
{
|
|
|
|
$configuration = $this->setupEnvironment();
|
|
|
|
$fields = array(
|
|
'app_id' => $configuration['appKey'],
|
|
'included_segments' => array('All'),
|
|
'headings' => array("en" => $notificationTitle),
|
|
'contents' => array(
|
|
"en" => $notificationContent
|
|
),
|
|
);
|
|
|
|
$fields = json_encode($fields);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
|
|
'Authorization: Basic ' . $configuration['secretKey']));
|
|
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);
|
|
curl_exec($ch);
|
|
curl_close($ch);
|
|
}
|
|
|
|
}
|