Forked from Pamblam/Sending Apple push notifications with APN service and .p8 key .php
Created
November 23, 2022 08:01
-
-
Save NaStillmatic/c0833f5c67b4fa88668d97e227b42ec9 to your computer and use it in GitHub Desktop.
Sending Apple push notifications with APN service and .p8 key
This file contains hidden or 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 | |
// Path to the .p8 file downloaded from apple | |
// see: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns#2943371 | |
$authKey = "AuthKey_S97G28Y3JP.p8"; | |
// Team ID (From the Membership section of the ios developer website) | |
// see: https://developer.apple.com/account/ | |
$teamId = 'asdfasdf'; | |
// The "Key ID" that is provided when you generate a new key | |
$tokenId = 'asdfasdf'; | |
// The Bundle ID of the app you want to send the notification to | |
$bundleId = 'com.companyname.appname'; | |
// Device token (registration id) | |
$devToken = 'asdfasdf908sd7f987sdf987asd9f87a9df790ads7f9afd7s90adf79a0fd7'; | |
// The content of the push notification | |
// see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1 | |
$notification_payload = [ | |
'alert' => [ | |
'title' => "Big Test", | |
'body' => "Big body test" | |
] | |
]; | |
// For development, turn to false | |
$production = true; | |
################################################################################ | |
## Generate JWT ################################################################ | |
################################################################################ | |
function b64($raw, $json=false){ | |
if($json) $raw = json_encode($raw); | |
return str_replace('=', '', strtr(base64_encode($raw), '+/', '-_')); | |
} | |
$token_key = file_get_contents($authKey); | |
$jwt_header = [ | |
'alg' => 'ES256', | |
'kid' => $tokenId | |
]; | |
$jwt_payload = [ | |
'iss' => $teamId, | |
'iat' => time() | |
]; | |
$raw_token_data = b64($jwt_header, true).".".b64($jwt_payload, true); | |
$signature = ''; | |
openssl_sign($raw_token_data, $signature, $token_key, 'SHA256'); | |
$jwt = $raw_token_data.".".b64($signature); | |
################################################################################ | |
## Send Message ################################################################ | |
################################################################################ | |
$request_body = json_encode($notification_payload); | |
$endpoint = $production ? 'https://api.push.apple.com/3/device': 'https://api.development.push.apple.com/3/device'; | |
$url = "$endpoint/$devToken"; | |
$ch = curl_init($url); | |
curl_setopt_array($ch, [ | |
CURLOPT_POSTFIELDS => $request_body, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, | |
CURLOPT_HTTPHEADER => [ | |
"content-type: application/json", | |
"authorization: bearer $jwt", | |
"apns-topic: $bundleId" | |
] | |
]); | |
$response = curl_exec($ch); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$err = curl_error($ch); | |
curl_close($ch); | |
################################################################################ | |
## Do something with output #################################################### | |
################################################################################ | |
header('Content-Type: text/plain'); | |
echo "-----"; | |
echo "HTTP Code: $httpcode\n"; | |
echo "cURL Error: ".var_export($err, true)."\n"; | |
echo "Response: \n$response\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment