Skip to content

Commit b256041

Browse files
Manoj kumarManoj kumar
authored andcommitted
start
0 parents  commit b256041

File tree

7 files changed

+265
-0
lines changed

7 files changed

+265
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SimpleApns
2+
3+
## _Apple Push Notification_
4+
5+
Needs
6+
- Config
7+
- Message
8+
- Device Token
9+
10+
Usage
11+
12+
```php
13+
<?php
14+
15+
require __DIR__ . '/vendor/autoload.php';
16+
17+
use SimpleApns\Apns;
18+
19+
$config =[
20+
'environment' => true,
21+
'keyPath' => './key.pem',
22+
'secretKey' => 'secret Key',
23+
'buildId' = 'build id'
24+
];
25+
26+
$message = [
27+
'title' => ' title ',
28+
'body' => 'body of message',
29+
'sound' => 'default' // optional
30+
];
31+
32+
$deviceToken = '64-bit token';
33+
34+
Apns::send($config, $message, $deviceToken);
35+
36+
```
37+
38+
Return response
39+
```php
40+
// success message
41+
array(2) {
42+
["response"]=>
43+
string(8) "Success."
44+
["code"]=>
45+
int(200)
46+
}
47+
48+
// fail
49+
array(2) {
50+
["response"]=>
51+
string(12) "Bad request."
52+
["code"]=>
53+
int(400)
54+
}
55+
```
56+
## License
57+
MIT

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "manojkumarlinux/simple-apns",
3+
"license": "MIT",
4+
"authors": [
5+
{
6+
"name": "Manoj Kumar Linux",
7+
"email": "manojkumardeveloper18@gmail.com"
8+
}
9+
],
10+
"autoload": {
11+
"psr-4": {
12+
"SimpleApns\\": "src/"
13+
}
14+
},
15+
"minimum-stability": "stable",
16+
"require": {}
17+
}

src/Apns.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace SimpleApns;
4+
5+
use SimpleApns\Lib\Config;
6+
use SimpleApns\Lib\Message;
7+
use SimpleApns\Lib\DeviceToken;
8+
use SimpleApns\Lib\Request;
9+
10+
class Apns {
11+
12+
public static function send($config, $message, $deviceToken) {
13+
$data = [];
14+
$setConfig = (new Config)->setKeyPath($config['keyPath'])->setSecretKey($config['secretKey'])->setBuildId($config['buildId'])->setEnv($config['environment']);
15+
$data['config'] = $setConfig->getConfig();
16+
$data['headers'] = $setConfig->getHaders();
17+
$setMessage = (new Message)->setTitle($message['title'])->setBody($message['body'])->setSound(isset($message['sound']) ? $message['sound'] : null);
18+
$data['message'] = $setMessage->getMessage();
19+
$setDeviceToken = (new DeviceToken)->setDeviceToken($deviceToken);
20+
$data['deviceToken'] = $setDeviceToken->getDeviceToken();
21+
return (new Request)->curlRequest($data);
22+
}
23+
24+
}

src/Lib/Config.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace SimpleApns\Lib;
4+
5+
class Config {
6+
7+
8+
private $urlProduction = 'https://api.push.apple.com/3/device/';
9+
10+
private $urlDevelopment = 'https://api.sandbox.push.apple.com/3/device/';
11+
12+
private $url;
13+
14+
private $keyPath;
15+
16+
private $buildId;
17+
18+
private $secretKey;
19+
20+
private $deviceToken;
21+
22+
23+
public function setEnv(bool $mode) {
24+
$this->url = $mode ? $this->urlProduction : $this->urlDevelopment;
25+
return $this;
26+
}
27+
28+
public function setKeyPath(string $path) {
29+
$this->keyPath = file_exists($path) ? $path : '';
30+
return $this;
31+
}
32+
33+
public function setSecretKey(string $key) {
34+
$this->secretKey = $key;
35+
return $this;
36+
}
37+
38+
public function setBuildId(string $id) {
39+
$this->buildId = $id;
40+
return $this;
41+
}
42+
43+
public function getConfig() {
44+
return [
45+
'secretKey' => $this->secretKey,
46+
'buildId' => $this->buildId,
47+
'url' => $this->url,
48+
'keyPath' => $this->keyPath,
49+
];
50+
}
51+
52+
public function getHaders() {
53+
return [
54+
'apns-topic: '.$this->buildId,
55+
'apns-push-type: alert',
56+
];
57+
}
58+
}
59+

src/Lib/DeviceToken.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SimpleApns\Lib;
4+
5+
class DeviceToken {
6+
private $deviceToken;
7+
8+
public function setDeviceToken(string $token) {
9+
$this->deviceToken = $token;
10+
return $this;
11+
}
12+
13+
public function getDeviceToken() {
14+
return $this->deviceToken;
15+
}
16+
}
17+
18+

src/Lib/Message.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace SimpleApns\Lib;
4+
5+
class Message {
6+
7+
private $title;
8+
9+
private $body;
10+
11+
private $sound = 'default';
12+
13+
public function setTitle(string $title) {
14+
$this->title = $title;
15+
return $this;
16+
}
17+
18+
public function setBody(string $body) {
19+
$this->body = $body;
20+
return $this;
21+
}
22+
23+
public function setSound(string $sound = null) {
24+
if(!is_null($sound))
25+
$this->sound = $sound;
26+
return $this;
27+
}
28+
29+
public function getMessage() {
30+
return [
31+
'aps' => [
32+
'alert' => [
33+
'title' => $this->title,
34+
'body' => $this->body,
35+
],
36+
'sound' => $this->sound,
37+
],
38+
];
39+
}
40+
41+
}

src/Lib/Request.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace SimpleApns\Lib;
4+
5+
class Request {
6+
7+
8+
public static function curlRequest(array $data) {
9+
10+
$responses = [
11+
'200' => 'Success.',
12+
13+
'400' => 'Bad request.',
14+
15+
'403' => 'There was an error with the certificate or with the provider’s authentication token.',
16+
17+
'404' => 'The request contained an invalid :path value.',
18+
19+
'405' => 'The request used an invalid :method value. Only POST requests are supported.',
20+
21+
'410' => 'The device token is no longer active for the topic.',
22+
23+
'413' => 'The notification payload was too large.',
24+
25+
'429' => 'The server received too many requests for the same device token.',
26+
27+
'500' => 'Internal server error.',
28+
29+
'503' => 'The server is shutting down and unavailable.',
30+
];
31+
32+
$url = $data['config']['url'].$data['deviceToken'];
33+
34+
$ch = curl_init($url);
35+
curl_setopt($ch, CURLOPT_HTTPHEADER, $data['headers']);
36+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data['message']));
37+
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
38+
39+
curl_setopt($ch, CURLOPT_SSLCERT, $data['config']['keyPath']);
40+
#curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $data['config']['secretKey']);
41+
42+
curl_setopt($ch, CURLOPT_FAILONERROR, true);
43+
curl_exec($ch);
44+
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
45+
curl_close($ch);
46+
47+
return [ 'response' => $responses[$httpcode], 'code' => $httpcode];
48+
}
49+
}

0 commit comments

Comments
 (0)