-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneSignalHelper.php
129 lines (113 loc) · 3.49 KB
/
OneSignalHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Vergie's Framework
*/
namespace Vetx;
class OneSignalHelper
{
/**
* Constructor
*
* @param string $app_id
* @param string $rest_api_key
*
* NOTE:
* This helper need curl support
*/
protected $app_id;
protected $rest_api_key;
protected $base_url = "https://onesignal.com/api/v1";
/* use for explode player ids */
protected $player_delimiter;
public function __construct($app_id, $rest_api_key)
{
$this->app_id = $app_id;
$this->rest_api_key = $rest_api_key;
$this->default_template = '';
$this->player_delimiter = '#';
}
public function setPlayerDelimiter($delimiter)
{
$this->player_delimiter = $delimiter;
}
public function sendToAll($message, $template_id = '')
{
$content = array(
"en" => $message
);
if ($template_id == '') {
$fields = array(
'app_id' => $this->app_id,
'included_segments' => array('All'),
'contents' => $content
);
} else {
$fields = array(
'app_id' => $this->app_id,
'included_segments' => array('All'),
'contents' => $content,
'template_id' => $template_id
);
}
$response = $this->request($fields);
return $response;
}
public function sendToUser($message, $player_id, $template_id = '')
{
$content = array(
"en" => $message
);
if ($template_id == '') {
$fields = array(
'app_id' => "",
'include_player_ids' => array($player_id),
'contents' => $content
);
} else {
$fields = array(
'app_id' => $this->app_id,
'include_player_ids' => array($player_id),
'contents' => $content,
'template_id' => $template_id
);
}
$response = $this->request($fields);
return $response . $player_id;
}
public function sendToUsers($message, $player_ids, $template_id = '')
{
$player_ids = explode($this->player_delimiter, $player_ids);
$content = array("en" => $message);
if ($template_id == '') {
$fields = array(
'app_id' => "",
'include_player_ids' => $player_ids,
'contents' => $content
);
} else {
$fields = array(
'app_id' => $this->app_id,
'include_player_ids' => $player_ids,
'contents' => $content,
'template_id' => $template_id
);
}
$response = $this->request($fields);
return $response . $player_id;
}
private function request($fields)
{
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url . "/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Basic ' . $this->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 $response;
}
}