Created
January 4, 2020 17:04
-
-
Save jstolp/ed08c43ee2afbf6b8148a2827d29dc24 to your computer and use it in GitHub Desktop.
APNs SamAuto PHP Push Script
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 | |
use Lcobucci\JWT\Signer\Key; | |
use Lcobucci\JWT\Signer\Ecdsa\Sha256; | |
use Lcobucci\JWT\Configuration; | |
$config = $container->get(Configuration::class); | |
assert($config instanceof Configuration); | |
/* | |
{ | |
“alg” : “ES256”, // Apple uses ES 256 | |
“kid” : “ABC123DEFG” // The encryption key, via developer.apple.com | |
“iss”: “DEF123GHIJ”, // Your team id (via developer.apple.com) | |
“iat”: 1437179036 // current unix timestamp (issuedAt) | |
} | |
*/ | |
$device_token = "device_token_here"; | |
$apns_topic = 'nl.samauto.ios-application'; | |
$p8file = "key_from_apple.p8"; | |
$key = new Key('file://' . __DIR__ . '/path-to-my-key-stored-in-a-file.pem'); | |
$token = (string) $config->createBuilder() | |
->issuedBy("DEF123GHIJ") // (iss claim) // teamId | |
->issuedAt(time()) // time the token was issuedAt | |
->withHeader('kid', "ABC123DEFG") | |
->setKey('file://' . $p8file) | |
->setSigner(new Sha256()) // APNs only supports the ES256 algorithm | |
->getToken(); // get the generated token | |
$payloadArray['aps'] = [ | |
'alert' => [ | |
'title' => "SamAuto.nl Push Notification", // title of the notification | |
'body' => "Visit SamAuto.nl for more awesome scripts", // content/body of the notification | |
], | |
'sound' => 'default', | |
'badge' => 1 | |
]; | |
$payloadJSON = json_encode($payloadArray); | |
$url = "https://api.sandbox.push.apple.com/3/device/$device_token"; | |
$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, ["Authorization: Bearer $token","apns-topic: $apns_topic"]); | |
$response = curl_exec($ch); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
// On successful response you should get true in the response and a status code of 200 | |
// A list of responses and status codes is available at | |
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1 | |
var_dump($response); | |
var_dump($httpcode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment