How to send iOS Push Notifications using TLS and PHP?

妖精的绣舞 提交于 2020-01-30 23:40:31

问题


My app is still in 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 was working but it was recently depreciated because Apple recently decided to drop SSL immediately affecting all apps in development and apps in production have until October 29th to change their code.

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

Here is what my php that used to work looks like:

$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 suggests:

$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 it still doesn't work. Do you have any suggestion to fix it?


回答1:


$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 localhost environment don't forget download the certification file entrust_2048_ca.cer




回答2:


<?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);



回答3:


Here are a few tips that should can help you figure out:

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

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

  3. Make sure if path is right: '../folder/file/ck.pem' ?

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

  5. Switch dev and production pem and try both.



来源:https://stackoverflow.com/questions/26577852/how-to-send-ios-push-notifications-using-tls-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!