Created
October 18, 2016 15:52
-
-
Save edamov/c1f70c744cd8c28d0a6870d5b50b77d6 to your computer and use it in GitHub Desktop.
PHP token-based (JWT) push notifications to APNS via HTTP/2
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
<?php | |
require_once 'vendor/autoload.php'; | |
use Jose\Factory\JWKFactory; | |
use Jose\Factory\JWSFactory; | |
$key_file = 'key.p8'; | |
$secret = null; // If the key is encrypted, the secret must be set in this variable | |
$private_key = JWKFactory::createFromKeyFile($key_file, $secret, [ | |
'kid' => 'ABCDEFGH12', // The Key ID obtained from your developer account | |
'alg' => 'ES256', // Not mandatory but recommended | |
'use' => 'sig', // Not mandatory but recommended | |
]); | |
$payload = [ | |
'iss' => 'JKLMNOPQRS', | |
'iat' => time(), | |
]; | |
$header = [ | |
'alg' => 'ES256', | |
'kid' => $private_key->get('kid'), | |
]; | |
$jws = JWSFactory::createJWSToCompactJSON( | |
$payload, | |
$private_key, | |
$header | |
); | |
var_dump($jws); | |
function sendHTTP2Push($http2ch, $http2_server, $app_bundle_id, $message, $token, $jws) { | |
// url (endpoint) | |
$url = "{$http2_server}/3/device/{$token}"; | |
// headers | |
$headers = array( | |
"apns-topic: {$app_bundle_id}", | |
'Authorization: bearer ' . $jws | |
); | |
// other curl options | |
curl_setopt_array($http2ch, array( | |
CURLOPT_URL => $url, | |
CURLOPT_PORT => 443, | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => $message, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_HEADER => 1 | |
)); | |
// go... | |
$result = curl_exec($http2ch); | |
if ($result === FALSE) { | |
throw new Exception("Curl failed: " . curl_error($http2ch)); | |
} | |
print_r($result); | |
// get response | |
$status = curl_getinfo($http2ch); | |
return $status; | |
} | |
// this is only needed with php prior to 5.5.24 | |
if (!defined('CURL_HTTP_VERSION_2_0')) { | |
define('CURL_HTTP_VERSION_2_0', 3); | |
} | |
// open connection | |
$http2ch = curl_init(); | |
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); | |
// send push | |
$message = '{"aps":{"alert":"Hi!","sound":"default"}}'; | |
$token = 'aaaaaaaa'; | |
$http2_server = 'https://api.development.push.apple.com'; // or 'api.push.apple.com' if production | |
$app_bundle_id = 'com.app.Test'; | |
sendHTTP2Push($http2ch, $http2_server, $app_bundle_id, $message, $token, $jws); | |
// close connection | |
curl_close($http2ch); |
I think he used only this lib JSON Object Signing and Encryption library
You can install it with Composer composer require spomky-labs/jose
This lib needs PHP 5.6
@edamov getting {"reason":"InvalidProviderToken"}
@edamov please modify this line https://gist.github.com/edamov/c1f70c744cd8c28d0a6870d5b50b77d6#file-index-php-L17, must changeTeam ID here
Hi, do you know how to deal with
"Uncaught Exception: Curl failed: GnuTLS recv error (-110): The TLS connection was non-properly terminated"
problem?
@edamov @vishalkalola1 @AlexanderMatveev i am receiving"reason":"BadDeviceToken" .. please help me
Get new device token from ios device and try again. @Talhasahi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how many dependency use in require_once 'vendor/autoload.php??