简体   繁体   中英

Send push notification to ios app : Google cloud

I have hosted my PHP code on google cloud.

I want to send push notifications to ios app. I have enabled port 2195 and 2196.

While sending the push notification I got the following error :

Warning: stream_socket_client(): SSL: Connection reset by peer

Warning: stream_socket_client(): Failed to enable crypto

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error)

I am not much familiar with Google Cloud. What should I do to make it working?

Here is 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 to 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 we are trying to send data to the APNS (Apple Push Notification Service) servers is the SSL certificates .

APNS uses this technology in order to serve more secure connection to its users.

As it is said at APNS documentation : " Each certificate is limited to a single app and is also limited to one of two development environments, each with its own assigned hostname ". So you can use two environments

  • Development (testing environment): ssl://gateway.sandbox.push.apple.com:2195

  • Production (once the app is launched): ssl://gateway.push.apple.com:2195

If you want to test if you can connect to APNS server, just try the following command:

$ telnet gateway.sandbox.push.apple.com 2195
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.

If you get an error then make sure your firewall allows outgoing connections on port 2195.

Then you can test if your SSL certificate and private key are working and it can be set up a secure connection:

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert YourDevCert.pem -key YourPrivateKey.pem
Enter pass phrase for YourPrivateKey.pem: ******

If this works it means that your certificates are correctly set up (you should see a whole bunch of output, which is openssl letting you know what is going on under the hood).

Once knowing all of this information, I see that you have one mistake in your code and also you should check something else:

  • Check if you have a good connection with APNS server.
  • Check that your $payload variable is a json string.
  • Check that you have a correct $deviceToken .
  • Check that you are using the right certificate with the right environtment. In this case, you are setting a apns-dev.pem certificate and you are sending it to the production environment (I interpret that your production certificate is apns-prod.pem so check it).
  • Check that your PHP file can find your certificate.
  • One of your problems , you haven't set any password for your private key. You should add the following line once you have added your certificate:

stream_context_set_option($ctx, "ssl", "passphrase", "your_private_key");

If you have some troubles or doubts, I followed this tutorial to send my first APNS Push Notifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM