Skip to content

Commit

Permalink
add the basic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef20000 authored and youssef20000 committed Apr 9, 2019
1 parent 10b0c8a commit 459ac29
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
98 changes: 98 additions & 0 deletions src/OneSignal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Created by PhpStorm.
* User: youssef
* Date: 4/9/19
* Time: 5:25 PM
*/

namespace Youssef\OneSignal;


class OneSignal
{

public $heading,$content,$logo,$image,$url,$appUrl,$webUrl,$buttons,$badge,$segment,$filters;

protected $apiKey;
protected $appId;
protected $fields;
public function __construct($appId=null, $apiKey=null)
{
if ($appId)
$this->apiKey = $apiKey;
else
$this->apiKey = config('onesignal.key');
if (appId)
$this->appId = $appId;
else
$this->appId = config("onesignl.id");
}

protected function prepare()
{
$app_id = $this->appId;
$rest_api_key = $this->apiKey;
$heading = $this->heading;
$content = $this->content;
$fields = array(
'app_id' => $app_id,
'contents' => $content,
'headings' => $heading
);
// if (!empty($extraParams)){
// $fields["data"]=$extraParams;
// }
if (!empty($this->logo)):
$fields['chrome_web_icon'] = $this->logo;
$fields['firefox_icon'] = $this->logo;
endif;
if ($this->image != null)
$fields['chrome_web_image'] = $this->image;
if ($this->badge != null)
$fields['chrome_web_badge'] = $this->badge;
$this->fields = $fields;
}

protected function exec(){
$fields = json_encode($this->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 ' . $rest_api_key
));
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["allresponses"] = $response;
return json_encode($return);
}

public function sendToAll(){
$this->prepare();
return $this->exec();
}

public function sendToSpecific($userId)
{
$this->prepare();
if ($userId != null) {
$this->fields['filters'] = array(
array(
'field' => 'tag',
'key' => 'userId',
'relation' => '=',
'value' => $userId
)
);
return $this->exec();
}
}
}
4 changes: 3 additions & 1 deletion src/OneSignalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function register()
*/
public function boot()
{
//
$this->publishes([
__DIR__.'onesignal.php' => config_path('onesignal.php')
], 'config');
}
}
5 changes: 5 additions & 0 deletions src/onesignal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return [
"app_id"=>env("ONESIGNAL_APP_ID"),
"api_key"=>env("ONESIGNAL_API_KEY")
];

0 comments on commit 459ac29

Please # to comment.