DEV Community

Discussion on: Connect to APNS via HTTP/2 with PHP

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.