Posts

Post not yet marked as solved
1 Replies
2.2k Views
Hi, everyone! I'm trying to do push notification for apple wallet pass via PHP. My code (in 2 variants) is below. According to guidelines - https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/PassKit_PG/Updating.html I should get the response in json format, but i don't. Anyway in the server logs I see the response 200. Any notification doesn't appear on the device. I also tried to push notification via command line tools, but also faced problems - the guideline - https://developer.apple.com/documentation/usernotifications/sending_push_notifications_using_command-line_tools give info only about push notification for your own app, not for the Wallet. I will be very grateful for any responses. №1 &#9;&#9;&#9;&#9;$apnsServer = 'tcp://api.push.apple.com:443/3/device/'; &#9;&#9; &#9;&#9;&#9;&#9;$privateKeyPassword = '<password>'; &#9;&#9;&#9;&#9;$message = 'test'; &#9;&#9;&#9;&#9;$deviceToken = &#9;&#9;&#9;&#9;'<token>'; &#9;&#9;&#9; &#9;&#9;&#9;&#9;$pushCertAndKeyPemFile = 'my.pem'; &#9;&#9;&#9;&#9;$stream = stream_context_create(); &#9;&#9;&#9;&#9;stream_context_set_option($stream, &#9;&#9;&#9;&#9;'ssl', &#9;&#9;&#9;&#9;'passphrase', &#9;&#9;&#9;&#9;$privateKeyPassword); &#9;&#9;&#9;&#9;stream_context_set_option($stream, &#9;&#9;&#9;&#9;'ssl', &#9;&#9;&#9;&#9;'local_cert', &#9;&#9;&#9;&#9;$pushCertAndKeyPemFile); &#9;&#9;&#9;&#9;$connectionTimeout = 20; &#9;&#9;&#9;&#9;$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT; &#9;&#9;&#9;&#9;$connection = stream_socket_client($apnsServer, &#9;&#9;&#9;&#9;$errorNumber, &#9;&#9;&#9;&#9;$errorString, &#9;&#9;&#9;&#9;$connectionTimeout, &#9;&#9;&#9;&#9;$connectionType, &#9;&#9;&#9;&#9;$stream); &#9;&#9;&#9;&#9;if (!$connection){ &#9;&#9;&#9;&#9;echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>"; &#9;&#9;&#9;&#9;exit; &#9;&#9;&#9;&#9;} else { &#9;&#9;&#9;&#9;echo "Successfully connected to the APNS. Processing...</br>"; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;$messageBody['aps'] = array('alert' => $message, &#9;&#9;&#9;&#9;'sound' => 'default', &#9;&#9;&#9;&#9;'badge' => 2, &#9;&#9;&#9;&#9;); &#9;&#9;&#9;&#9;$payload = json_encode($messageBody); &#9;&#9;&#9;&#9;$notification = chr(0) . &#9;&#9;&#9;&#9;pack('n', 32) . &#9;&#9;&#9;&#9;pack('H*', $deviceToken) . &#9;&#9;&#9;&#9;pack('n', strlen($payload)) . &#9;&#9;&#9;&#9;$payload; &#9;&#9;&#9;&#9;$wroteSuccessfully = fwrite($connection, $notification, strlen($notification)); &#9;&#9;&#9;&#9;$apple_error_response = fread($connection, 6); &#9;&#9;&#9;&#9;if (!$wroteSuccessfully){ &#9;&#9;&#9;&#9;echo "Could not send the message<br/>"; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;else { &#9;&#9;&#9;&#9;echo "Successfully sent the message<br/>"; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;fclose($connection); ?> №2 $apnsCert = 'my.pem'; $push_token = '<token>'; $passIdentify = '<pass identify>'; $payload['aps'] = array('alert' => 'Oh hai!', 'badge' => 1, 'sound' => 'default'); $output = json_encode($payload); $msg = chr(0) . pack('n', 32) . pack('H*', $push_token) . pack('n', strlen($output)) . $output . pack('n', strlen($passIdentify)) . $passIdentify; $streamContext = stream_context_create(); stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert); $apns = stream_socket_client("api.push.apple.com:443/3/device/", $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext); if (!$apns) exit ("APNS Connection Failed: $error $errorString" . PHP_EOL); var_dump($apns); if(fwrite($apns, $msg)) { &#9;&#9;echo "ok"; } else { &#9;&#9;echo "not ok ".error_reporting(E_ALL); } socket_close($apns); fclose($apns);
Posted
by juljul.
Last updated
.