php - Send push notification to ios app : Google cloud -
i have hosted php code on google cloud.
i want send push notifications ios app. have enabled port 2195 , 2196.
while sending push notification got following error :
warning: stream_socket_client(): ssl: connection reset peer
warning: stream_socket_client(): failed enable crypto
warning: stream_socket_client(): unable connect ssl://gateway.push.apple.com:2195 (unknown error)
i not familiar google cloud. should make working?
here code:
$ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', pem_file_path . 'apns-dev.pem'); $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, stream_client_connect, $ctx); if (!$fp) { $data['msgs'] = "failed connect $err $errstr \n"; } else { $payload = json_encode($body); $msg = chr(0) . pack("n", 32) . pack("h*", str_replace(" ", "", $devicetoken)) . pack("n", strlen($payload)) . $payload; $result = fwrite($fp, $msg); if (!$result) { $data['msgs'] = 'message not delivered'; //. php_eol; } else { $data['msgs'] = 'success'; //. php_eol; } fclose($fp); } return $data;
the main problem when trying send data apns (apple push notification service) servers ssl certificates.
apns uses technology in order serve more secure connection users.
as said @ apns documentation: "each certificate limited single app , limited 1 of 2 development environments, each own assigned hostname". can use 2 environments
development (testing environment):
ssl://gateway.sandbox.push.apple.com:2195
production (once app launched):
ssl://gateway.push.apple.com:2195
if want test if can connect apns server, try following command:
$ telnet gateway.sandbox.push.apple.com 2195 trying 17.172.232.226... connected gateway.sandbox.push-apple.com.akadns.net. escape character '^]'.
if error make sure firewall allows outgoing connections on port 2195.
then can test if ssl certificate , private key working , can set secure connection:
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert yourdevcert.pem -key yourprivatekey.pem enter pass phrase yourprivatekey.pem: ******
if works means certificates correctly set (you should see whole bunch of output, openssl letting know going on under hood).
once knowing of information, see have 1 mistake in code , should check else:
- check if have connection apns server.
- check
$payload
variable json string. - check have correct
$devicetoken
. - check using right certificate right environtment. in case, setting
apns-dev.pem
certificate , sending production environment (i interpret production certificateapns-prod.pem
check it). - check php file can find certificate.
- one of your problems, haven't set password private key. should add following line once have added certificate:
stream_context_set_option($ctx, "ssl", "passphrase", "your_private_key");
if have troubles or doubts, followed this tutorial send first apns push notifications.
Comments
Post a Comment