-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapns.inc.php
More file actions
66 lines (52 loc) · 1.75 KB
/
apns.inc.php
File metadata and controls
66 lines (52 loc) · 1.75 KB
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
<?php
require_once("config.inc.php");
require_once("vendor/MultibyteStringConverter.php");
function get_apns_key() {
$apnsSecret = file_get_contents(__DIR__ . "/" . APNS_KEY_FILE);
return openssl_pkey_get_private($apnsSecret);
}
function jwt_base64_encode($data) {
$result = base64_encode($data);
$result = strtr($result, "+/", "-_");
return rtrim($result, "=");
}
function generate_apns_token() {
$jsonHeader = array(
"alg" => "ES256",
"kid" => APNS_KEY_ID
);
$jsonPayload = array(
"iss" => APNS_TEAM_ID,
"iat" => time()
);
$apnsKey = get_apns_key();
$jsonHeaderEncoded = jwt_base64_encode(json_encode($jsonHeader));
$jsonPayloadEncoded = jwt_base64_encode(json_encode($jsonPayload));
$tokenProtectedPart = $jsonHeaderEncoded . "." . $jsonPayloadEncoded;
$converter = new MultibyteStringConverter();
$jsonSignatureAsn1 = "";
openssl_sign($tokenProtectedPart, $jsonSignatureAsn1, $apnsKey, OPENSSL_ALGO_SHA256);
openssl_free_key($apnsKey);
$jsonSignature = $converter->fromAsn1($jsonSignatureAsn1, 64);
return
$tokenProtectedPart . "." .
jwt_base64_encode($jsonSignature);
}
function send_apns_push($environment, $payload, $apnsToken, $deviceToken, $bundleID) {
$payloadJSON = json_encode($payload);
$environmentURL = "https://api.sandbox.push.apple.com";
if ($environment == 1) {
$environmentURL = "https://api.push.apple.com";
}
$url = "$environmentURL/3/device/$deviceToken";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJSON);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $apnsToken",
"apns-topic: $bundleID"
));
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $httpcode == 200;
}