Reputation: 21
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);
?>
Upvotes: 2
Views: 2210
Reputation: 6954
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
Hope this help you
Upvotes: 1