2

I am having some trouble with my push notifications. I have read through heaps of tutorials and have come up with the script below. I can't seem to get a connection to the APNs. Just to clarify, I am not coding the app side code for the app. I only know PHP and other web based languages.

I have gone throughout the process of creating the .pem file form my certificate and private key. I also have a SSL certificate for my web server (I did this through my web host provider, not sure if that is the right way about doing this). I am just not sure exactly what has to happen in order to test that everything is working fine.

When I load the script on the web page i get the error: Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) Failed to connect: 110 Connection timed out

Can you please look through the code and help me find the reason that I am not able to connect to APNs. Also is there a way i can get a more details error report?

Here is my code: (The problem should be in the first 15 or so lines.)

<?PHP
$username = $_GET['username'];
$userIDarr = $_GET['userARR'];
$message = $username.' is d2P';
$passphrase = 'mypass'; // not sure what this refers too.. (from what i have read I think it is meant to be for the iphone side of things)

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

// Open a connection to the APNS server
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 10, STREAM_CLIENT_CONNECT, $streamContext);

if (!$apns)
    exit("Failed to connect: $error $errorString" . PHP_EOL);
echo 'Connected to Apple service. ' . PHP_EOL;

@mysql_connect ("localhost","down2par_down2pa","4m329aMh") or die ("could not connect to MySQL");
@mysql_select_db ("down2par_d2pdb") or die ("no database");

// create array of all device IDs AND badges that will be receiving notifications
$SQLarr = implode(" AND userid =", $userIDarr);
$DB = mysql_query("SELECT diviceID, badge FROM new_fb_users WHERE userid = '$SQLarr'");



while($DBarr = mysql_fetch_array($DB)) {

    $deviceToken = $DBarr['deviceID'];
    $badge = $DBarr['badge'];
    $id = $DBarr['id'];

    mysql_query("UPDATE new_fb_users SET badge = badge+1 WHERE id = $id");

    // create the payload body
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default',
        'badge' => ($badge > 0 ? $badge + 1 : 1)
        );
    // Encode the payload as JSON
    $payload = json_encode($body);

    // Build the binary notification
    $msg = chr(0).pack('n', 32).pack('H*', str_replace(' ', '', $deviceToken)).pack('n', strlen($payload)).$payload;
    // Send it to the server
    $result = fwrite($apns, $msg, strlen($msg));
    if (!$result)
        echo 'Failed message'.PHP_EOL;
    else
        echo 'Successful message'.PHP_EOL;
}

// Close the connection to the server
fclose($apns);
?>
  • You are **wide open** to SQL injection attacks, and you **will be hacked** if you haven't been already. Learn to use prepared/parameterized queries with PDO or similar to avoid this problem entirely. – Brad Mar 08 '13 at 04:28
  • Check this link. It helped me.. http://stackoverflow.com/questions/1481443/apple-push-notification-service – shivam Mar 08 '13 at 04:42
  • Hey Brad, thanks for the tip. I was planning on getting that done before I launched the update. – Artist Link Mar 08 '13 at 05:55

1 Answers1

1

Passphase is a secret key that you enter while creating p12 file. Recal what you entered when you exported your p12 file. For more details refer this tutorial

refer this link

Hope this help you

Community
  • 1
  • 1
DivineDesert
  • 6,924
  • 1
  • 29
  • 61
  • Hi, That is the tutorial I used to create my .pem file. I followed all steps and executed this test line : – Artist Link Mar 08 '13 at 05:43
  • (accidentally added above comment half way through) $ telnet gateway.sandbox.push.apple.com 2195 Trying 17.172.232.226... Connected to gateway.sandbox.push-apple.com.akadns.net. Escape character is '^]'. – Artist Link Mar 08 '13 at 05:45
  • And the Test was successful, But when i did the next test: $ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem Enter pass phrase for PushChatKey.pem: I was not successful. I think it might have something to do with my SSL.. It didn't mention anything in that tutorial about what to do with your SSL once you get it. Am I supposed to include that somewhere? – Artist Link Mar 08 '13 at 05:46
  • have u changed your passphase ? – DivineDesert Mar 08 '13 at 05:56
  • No, not that I am aware of. Is there a way to check? – Artist Link Mar 08 '13 at 06:23
  • You need to change this line `$passphrase = 'mypass'; // not sure what this refers too.. (from what i have read I think it is meant to be for the iphone side of things)`. This you cant get from any where AFAIK. If you have created p12 file then recall what you entered else contact person who created that file – DivineDesert Mar 08 '13 at 06:49
  • Ok so I need to add my password that I set as the passphrase variable? But the other thing is, that variable is not being used anywhere. Where should I include it in the code? – Artist Link Mar 08 '13 at 10:31
  • Oops you need this line as well `stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);` – DivineDesert Mar 08 '13 at 11:13