Last active
April 29, 2024 12:29
-
-
Save valfer/18e1052bd4b160fed86e6cbb426bb9fc to your computer and use it in GitHub Desktop.
Sending Push Notification with HTTP2 (and PHP) see entire post at: http://coding.tabasoft.it/ios/sending-push-notification-with-http2-and-php/
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 | |
/** | |
* @param $http2ch the curl connection | |
* @param $http2_server the Apple server url | |
* @param $apple_cert the path to the certificate | |
* @param $app_bundle_id the app bundle id | |
* @param $message the payload to send (JSON) | |
* @param $token the token of the device | |
* @return mixed the status code (see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW18) | |
*/ | |
function sendHTTP2Push($http2ch, $http2_server, $apple_cert, $app_bundle_id, $message, $token) { | |
$milliseconds = round(microtime(true) * 1000); | |
// url (endpoint) | |
$url = "{$http2_server}/3/device/{$token}"; | |
// certificate | |
$cert = realpath($apple_cert); | |
// headers | |
$headers = array( | |
"apns-topic: {$app_bundle_id}", | |
"User-Agent: My Sender" | |
); | |
// other curl options | |
curl_setopt_array($http2ch, array( | |
CURLOPT_URL => "{$url}", | |
CURLOPT_PORT => 443, | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => $message, | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_SSLCERT => $cert, | |
CURLOPT_HEADER => 1 | |
)); | |
// go... | |
$result = curl_exec($http2ch); | |
if ($result === FALSE) { | |
throw new Exception('Curl failed with error: ' . curl_error($http2ch)); | |
} | |
// get respnse | |
$status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE); | |
$duration = round(microtime(true) * 1000) - $milliseconds; | |
// echo $duration; | |
return $status; | |
} | |
// open connection | |
if (!defined('CURL_HTTP_VERSION_2_0')) { | |
define('CURL_HTTP_VERSION_2_0', 3); | |
} | |
$http2ch = curl_init(); | |
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); | |
// send push | |
$apple_cert = '/certificates/samplepush/development.pem'; | |
$message = '{"aps":{"alert":"Hi!","sound":"default"}}'; | |
$token = 'dbdaeae86abcde56rtyww1859fb41d2c7b2cberrttyyy053ec48987847'; | |
$http2_server = 'https://api.development.push.apple.com'; // or 'api.push.apple.com' if production | |
$app_bundle_id = 'it.tabasoft.samplepush'; | |
// close connection | |
for ($i = 0; $i < 20; $i++) { | |
$status = sendHTTP2Push($http2ch, $http2_server, $apple_cert, $app_bundle_id, $message, $token); | |
echo "Response from apple -> {$status}\n"; | |
} | |
curl_close($http2ch); |
You can test your certificates at that link https://pushtry.com/
What is this $http2ch param for ? And one more thing what to pass in $app_bundle_id?
400 error. Any idea?
I got "security library failure"
hi does anyone still maintains this? im getting response 200 but no notification is showing on my safari macOS. If anyone can help me pls. thank you
hi does anyone still maintains this? im getting response 200 but no notification is showing on my safari macOS. If anyone can help me pls. thank you
In my case this was due to the fact that the $message
was not correctly formatted.
$message = ['aps' => ['alert' => ['title' => '...', 'body' => '...'], 'url-args' => ['']]];
$message = json_encode($message);
OP I tried using this site https://pushtry.com/. Here I observed that when I use p12 file it worked but when I used .pem file instead , it does worked. Any help ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to check certificate is working or not?