-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
youssef20000
authored and
youssef20000
committed
Apr 9, 2019
1 parent
10b0c8a
commit 459ac29
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
]; |