How to send iOS Push notifications using TLS and PHP?

My application is still under development and I used this tutorial to send iOS Push notifications using PHP and SSL.

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

It worked, but recently it depreciated because Apple recently decided to abandon SSL, immediately affecting all applications in development and applications in production, until October 29 to change their code.

I would like to know how to do the same using TLS instead of SSL.

Here's how my php works, which worked:

$ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

I tried adding an Entrust certificate as Apple offers:

 $ctx = stream_context_create(); stream_context_set_option($ctx, 'tls', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'tls', 'passphrase', $passphrase); stream_context_set_option($ctx, 'tls', 'cafile', 'entrust_2048_ca.cer'); $fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

But that still doesn't work. Do you have any suggestion to fix this?

+6
source share
3 answers
 $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer'); $fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if you work in a local environment, do not forget to download the certification file entrust_2048_ca.cer

+3
source
 <?php $message = 'aa_' . rand(10000,99999); $deviceToken = array( 'xxxxxx' ); $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'passphrase', '111111'); stream_context_set_option($ctx, "ssl", "local_cert", './apns.pem'); $fp = NULL; $errno = NULL; $errstr = NULL; $fp = stream_socket_client("tls://gateway.sandbox.push.apple.com:2195", $errno, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if($fp === FALSE){ exit('error message'); } $content = array("aps" => array("alert" => $message, "badge" => 4, "sound" => 'default', "code" => 200)); $data = json_encode($content); foreach ($deviceToken as $token) { $msg = chr(0) . pack("n", 32) . pack("H*", $token) . pack("n", strlen($data)) . $data; fwrite($fp, $msg); fflush($fp); } fclose($fp); 
+2
source

Here are some tips to help you figure out:

  • Go to entrust.net/downloads/root_request.cfm and download entrust_2048_ca.cer

  • Add the following code: stream_context_set_option ($ ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');

  • Make sure the correct path is: '../folder/file/ck.pem'?

  • Switch and try both the sandbox and ssl live links.

  • Switch dev and production program and try both.

0
source

Source: https://habr.com/ru/post/977247/


All Articles