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...
For further actions, you may consider blocking this person and/or reporting abuse
If you're trying to get this to work in 2021:
missing container usage can be replaced by
And the builder signature changed to:
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,
]);
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.
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?
Where does $container come from?
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.
Those who are having the same issue just like I had. Add this code in the curl code and it will work.
Don't forget there is a great package out there that can do this all: github.com/edamov/pushok
Where does the $container comes?
I am getting error with $container.
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.
I am getting 400 code on the server and 200 on my local end. How to fix it?