After receiving the deviceToken sent from the App and the push payload from the product manager, we, as server programmers, need to generate and encrypt the JSON Web Token (JWT) before sending all three together to APNs.

The concept of JWT authentication tokens and how to obtain a signature Key from the Apple Developer site are covered in my tutorial for those unfamiliar.

How to use PHP on the server side

1. Generate and encrypt JWT

2. Send a push request to the APNs

Use framework: jwt-framework

1. Generate and encrypt JWT

Install the framework package in Composer:

composer require web-token/jwt-framework

Introduce:

<? php require_once'vendor/autoload.php';
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\Converter\StandardConverter;
use Jose\Component\KeyManagement\JWKFactory;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Algorithm\ES256;
use Jose\Component\Signature\Serializer\CompactSerializer;
Copy the code

Create JWT:

<? phpfunction getToken($cerPath.$keyID.$teamId) {// 1. Create the ES256 algorithm$algorithmManager= AlgorithmManager::create([ new ES256() ]); // 2. Create JSON Web key from p8 file (signature key) created on Apple developer website$cerPathIs the path of the p8 file)$jwk = JWKFactory::createFromKeyFile($cerPath); // JSON converter$jsonConverter= new StandardConverter(); // Instantiate JWSBuilder$jwsBuilder = new JWSBuilder(
	    $jsonConverter.$algorithmManager); // create the payload part of JWT (iat is the current timestamp, iss is the developer account TeamID)$payload = $jsonConverter->encode([
	    'iat' => time(),
	    'iss'= >$teamId,]); // 4. Generate and encrypt JWT$jws = $jwsBuilder->create();$payload// Set payload ->addSignature($jwk['alg'= >'ES256'.'kid'= >$keyID] // Encrypt with JSON Web key and Apple specified JWTheader signature ->build(); / / generated$serializer = new CompactSerializer($jsonConverter); // Serialization tool // 5. Serialize JWT$token = $serializer->serialize($jws); // serialize the above JWS, there is only one signature, so index 0 is okreturn $token;
}
Copy the code

2. Send a push request to the APNs

<? phpfunction sendPush($deviceToken.$authToken.$payload) {// 1. Create a path (essentially a string) with deviceToken - Apple requires it$path = '/3/device/'.$deviceToken;
	
	$curl = curl_init();
	
	curl_setopt_array($curlCURLOPT_URL => CURLOPT_URL =>"https://api.development.push.apple.com:443".$path,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_CUSTOMREQUEST => "POST"// Add the payload to the request body.$payload,
	  CURLOPT_HTTPHEADER => array(
	    "Content-Type: application/json"."apns-expiration: 0"."apns-push-type: alert"."apns-topic: com.example.app", // 4. BundleID of our App"authorization: bearer ".$authToken// 5. JWT),)); // Send the request$response = curl_exec($curl);
	
	$err = curl_error($curl);
	curl_close($curl);
	
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
	  echo $response; }}Copy the code

Call the above two functions and pass the parameters:

<? php$token = getToken('key/path/mykey.p8'.'Key ID'.'Team ID');
$payload = json_encode(['aps'= > ['alert'= >'Hi there! ']]);
sendPush('9fbb61136e03c................. '.$token.$payload);
Copy the code