-
-
Save Pamblam/f484b6e99dbea72effc4f304f097e039 to your computer and use it in GitHub Desktop.
| <?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"; |
@strphone hi, check your $bundleId. apple gives strange error if $bundleId is wrong
@strphone did you found a solution for this? Before 2 days my working .pem certificate expired. I decide to rewrite it with .p8 certificate and now didn't get any push notifications.
Response from call is same as yours. Code 200, no Errors but not a push
how to get the device-id? it's obviously not the UUID I register in my apple developer account under test devices? Thanks
Which device-id ? If you want to send push notification you must ask the user to grant the push notification authorization ad then you will get it. Do that in your AppDelegate didFinishLaunchingWithOptions
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { //User Notification granted } else { self.monutilisateur.isGranted = NO; //User Notification permission denied } }];
hi
thank you for your code.
i just need help if you can because i got a result 200 but the push does not arrive.
thanks
-----HTTP Code: 200
cURL Error: ''
Response:
1