views:

1936

answers:

3

I've followed the instructions at How to build an Apple Push Notification provider server (tutorial) in an attempt to set up a push notification server.

When I attempt to connect with the following PHP code

// connect to apns server
$strAPNSUrl = 'ssl://gateway.sandbox.push.apple.com:2195;
$strAPNSCert = 'dev.pem';

// generate stream
$oStreamContext = stream_context_create();
stream_context_set_option($oStreamContext, 'ssl', 'local_cert', $strAPNSCert);

// create the socket connection
$oAPNS = stream_socket_client($strAPNSUrl, $iError, $strError, 2, STREAM_CLIENT_CONNECT, $oStreamContext);

I get the following warning

Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert

Based on what information I've been able to find on Google, the issue seems to be my certificate.

I'm generating my csr and pem files in OS X (pem files per the instructions in the tutorial). Once I have created dev.pem I am uploading it to my hosting provider and attempting to run my php script. Is this the correct way to create and "install" the certificate?

I've run out of debugging ideas. Any direction would be great.

+2  A: 

Path to dev.pem was incorrect on my server.

The instructions are indeed the correct way to generate the .pem file. Once created it can be used on any machine (not just the machine that generated it).

Jason George
A: 

Hey Jason,

Were you able to use the same .pem file in your server? And as you said...on any machine.

I have follow this tutorial and I could send messages from my machine (the one were the certificate was generated) but I have tried copy all files and run the script in another machine and I have got the same errors that you have got.

How have you done to make it works in another machine? Have you installed the .pem file? Any special config?

Cheers

vfn
As far as "installing" the .pem file, all I had to do was copy it to a local directory on my server. The issue was I had moved my php script to a cron directory where I hold my cron jobs, and failed to copy over the .pem file. Once I moved the .pem file all was well. My hosting provider is running linux, so the .pem file should be good for any platform.
Jason George
File permissions could potentially be an issue. My .pem is set to 644.
Jason George
@Jason GeorgeI changed the file permissions but the error didn't change.I have done almost the same thing that you've said. I have configured my machine, tested and everything has been working. So, I have tried to copy everything and put in another mac that I have, but in this machine I am getting this error.
vfn
In this doc (http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ProvisioningDevelopment/ProvisioningDevelopment.html#//apple_ref/doc/uid/TP40008194-CH104-SW1) Apple says that the .p12 must be installed in the new server/machine. Have you done that or only copy/paste run.
vfn
Hey all,I could figure out the problem.....my mystake was run the script from a different location. it means, I was in the folder A and trying to rum my script like >php -f ../B/apns.php "message with badge" 4. When I've tried to do >php -f apns.php "message with badge" being under folder B it has stared to work.
vfn
A: 

I also followed "How to build an Apple Push Notification provider server (tutorial)" in an attempt to set up a push notification server and able to send push notification using my local system (system on which certificates generated).

But when I moved same files to my web server they never work. Following is the copy/paste code of tutorial. Last message they print on the screen is "Trying to open connection". I did not get any further message no exception and even no "Failed to connect..." message.

I also uploaded .p12 files (certificate and Key) on server but still did not work.

Do I need more settings/config on the server? What I am missing. I spent two days to figuring it out but still no clue.

It is working on my machine but not on server. where is the issue?

I placed all my files in one folder.

  1. apns.php
  2. apnsdev.pem
  3. apnsdevcert.p12
  4. apnsdevkey.p12

Trying to run apns.php like http://www.myserver.com/apntesting/apns.php

//FOLLOWING IS THE CODE

$deviceToken = 'XXXXX XXXXXX ';
$uniqueId = '0b9214ea';

$payload['aps'] = array('alert' => 'You have received message.', 'badge' => 1, 'sound' => 'Glass.caf');
$payload['server'] = array('UniqueId' => $uniqueId);
$payload = json_encode($payload);
echo "After JSON Encode";

//*************************************
// TESTING ON WEB SERVER
//*************************************
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = '2195';
$apnsCert = 'apnsdev.pem';
$passPhrase = '';

echo "Creating stream object.";
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $passPhrase);

try {
echo "Trying to open connection";

$apnsConnection = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

if (apnsConnection == false) {
    echo "Failed to connect {$error} {$errorString}\n";
    print "Failed to connect {$error} {$errorString}\n";
    return;
}

echo "construct apnsMessage";
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
echo "Message: $apnsMessage<br />\n";

fwrite($apnsConnection, $apnsMessage);

socket_close($apnsConnection);
fclose($apnsConnection);
}
catch(Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}
syedhassan4
Does the below answer work for you? If not I would suggest creating your own question. :)
bobber205