DEV Community

Cover image for Connect to APNS via HTTP/2 with PHP

Connect to APNS via HTTP/2 with PHP

SaMauto on January 04, 2020

Send a push message via APNs with a PHP Script HTTP/2 is the new standard (and from November 2020 onwards the only way to connect to the...
Collapse
 
smillerdev profile image
Sean Molenaar •

If you're trying to get this to work in 2021:
missing container usage can be replaced by

$config = Configuration::forUnsecuredSigner();
Enter fullscreen mode Exit fullscreen mode

And the builder signature changed to:


$token = (string) $config->builder()
                         ->issuedBy("teamId") // (iss claim) // teamId
                         ->issuedAt(new DateTimeImmutable()) // time the token was issuedAt
                         ->withHeader('kid', "keyId")
                         ->getToken(new Sha256(), new Key\LocalFileReference('file://' . $p8file)); // get the generated token
Enter fullscreen mode Exit fullscreen mode
Collapse
 
deepak_panwar_1 profile image
Deepak Panwar •

Just use GuzzleHttp\Client and we don't need JWT. See the working code-

$url = "api.sandbox.push.apple.com/3/device/";

$headers = array(
"apns-topic: com.example.exampleapp",
"apns-push-type: alert",
"Content-Type: application/x-www-form-urlencoded",
);

$certificate_file = "iosCertificates/apple-push-dev-certificate-with-key.pem";

$payloadArray['aps'] = [
'alert' => [
'title' => "Test Push Notification",
'body' => "Ohhh yeah working", ],
'sound' => 'default',
'badge' => 1

];

$data = json_encode($payloadArray);

$client = new Client();

$response = $client->post($url, [
'headers' => $headers,
'cert' => $certificate_file,
'curl' => [
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
],
'body'=> $data,

]);

Collapse
 
nishittops profile image
nishittops •

I tries this. Getting following error:
Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to api.sandbox.push.apple.com port 80: Connection timed out
Am I missing anything here?

Collapse
 
akebar__93923a3326cb958c1 profile image
Akebar •

This is the correct answer. I was able to use this to send Safari Web Push Notifications.

Ensure that you include the device token in the URL:
$url = "api.sandbox.push.apple.com/3/devic...", otherwise it won't work.

Collapse
 
fodorbalint profile image
fodorbalint •

Where does $container come from?

Collapse
 
gustavguez profile image
Gustavo Rodríguez •

lcobucci-jwt.readthedocs.io/en/lat...

The examples here fetch the configuration object from a hypothetical dependency injection container. You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped.

Collapse
 
vipera177 profile image
Hemant Kumar •

Those who are having the same issue just like I had. Add this code in the curl code and it will work.

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
samauto profile image
SaMauto •

Don't forget there is a great package out there that can do this all: github.com/edamov/pushok

Collapse
 
imtiyaz profile image
Mohammed Imtiyaz •

Where does the $container comes?

Collapse
 
slarkjm0803 profile image
slarkjm0803 •

I am getting error with $container.

Collapse
 
vipera177 profile image
Hemant Kumar •

I am getting 400 code on the server and 200 on my local end. How to fix it?

Collapse
 
randor profile image
Randy Mennie •

Did anyone get an answer as to where $container comes from? I get no response whatsoever back from this script. I guess none of this actually works.