Post not yet marked as solved
Post marked as unsolved with 1 replies, 2,229 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
				$apnsServer = 'tcp://api.push.apple.com:443/3/device/';
		
				$privateKeyPassword = '<password>';
				$message = 'test';
				$deviceToken =
				'<token>';
			
				$pushCertAndKeyPemFile = 'my.pem';
				$stream = stream_context_create();
				stream_context_set_option($stream,
				'ssl',
				'passphrase',
				$privateKeyPassword);
				stream_context_set_option($stream,
				'ssl',
				'local_cert',
				$pushCertAndKeyPemFile);
				$connectionTimeout = 20;
				$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
				$connection = stream_socket_client($apnsServer,
				$errorNumber,
				$errorString,
				$connectionTimeout,
				$connectionType,
				$stream);
				if (!$connection){
				echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
				exit;
				} else {
				echo "Successfully connected to the APNS. Processing...</br>";
				}
				$messageBody['aps'] = array('alert' => $message,
				'sound' => 'default',
				'badge' => 2,
				);
				$payload = json_encode($messageBody);
				$notification = chr(0) .
				pack('n', 32) .
				pack('H*', $deviceToken) .
				pack('n', strlen($payload)) .
				$payload;
				$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
				$apple_error_response = fread($connection, 6);
				if (!$wroteSuccessfully){
				echo "Could not send the message<br/>";
				}
				else {
				echo "Successfully sent the message<br/>";
				}
				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)) {
		echo "ok";
} else {
		echo "not ok ".error_reporting(E_ALL);
}
socket_close($apns);
fclose($apns);