0

I have correctly setup my provisioning profile and application to receive push notifications, but I am struggling with using PHP to send the notification. I am using the script as outlined below, but am receiving this warning:

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused).

I have done a bunch of research on this problem but am not sure how to resolve this issue. Any Ideas would be very helpful. THank you all!

<?php

$message=$_POST['message'];

if ($message)
{

    $device_token = "TOKEN_HERE";

    $payload = '{
    "aps": {
         "badge": 1,
         "alert": "Hello world!",
         "sound": "bingbong.aiff"
    }
}';

$ctx = stream_context_create();
stream_context_set_option ($ctx, 'ssl', 'local_cert', 'PEM_FILE_HERE' );
stream_context_set_option ($ctx, 'ssl', 'passphrase', 'PASSPHRASE_HERE' );
$fp = stream_socket_client ('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 
60, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) {
print "Failed";
return;
}
else {
print "Sent";
}

$devArray = array();
$devArray[] = $deviceToken;

foreach($devArray as $deviceToken) {
$msg = chr(0) . pack("n",32) . pack ('H*', str_replace(' ', '', $deviceToken)) . pack 
("n",strlen($payload)) . $payload . "n";
print "Sending Message :" . $payload . "n";
fwrite($fp,$msg);
}
fclose($fp);

}
?>
<form action="" method="post">
<input type = "text" name = "message">
<input type = "submit" value = "Send Notification">
</form>
4
  • It may be your network connection issue ! Try creating socket for any other service and test weather socket creation is allowed or not ? Commented Jun 17, 2013 at 4:35
  • ensure pem file name and passphrase name are correct Commented Jun 17, 2013 at 4:38
  • 1
    sounds like a firewall problem. Commented Jun 17, 2013 at 4:43
  • @DevZer0 I think you are correct. I did some digging around and it appears that my shared GoDaddy hosting does not open port 2195. Hmmmmm. Commented Jun 17, 2013 at 4:59

1 Answer 1

1

Here is a working script

 class Send_Push_Apple
 {
     public $apnsCert = 'PushProductionCertificatesWithKey.pem';


 function __construct()
  {
 /*
  * Nothing to fo Here
  *   
  * */ 

  }
static function send_push($data=array())
  {

    $payload['aps'] = array('alert' => array('body' => $data['message'])); 
    $payload['aps']['badge'] = 1; 
    $payload['aps']['sound'] = 'default'; 
    $payload = json_encode($payload); 
    //For  development 
    //$deviceToken = '4e6ec248724ef635e#############################';  
    $deviceToken = $data['device_token'];

    //$apnsCert = 'DistPEMCertificate-05-05.pem'; 
    $streamContext = stream_context_create(); 
    //stream_context_set_option($streamContext, 'ssl', 'local_cert',$apnsCert); 
    $apnsCert = 'PushProductionCertificatesWithKey.pem';
    stream_context_set_option($streamContext, 'ssl', 'local_cert',$apnsCert); 

    //ssl://gateway.sandbox.push.apple.com:2195
    $apns = stream_socket_client('ssl://gateway.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext); 

    if (!$apns) {
        print "Failed to connect $error $errorString\n";
        return;
    } else {
        print "Connection OK";
    }

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload; 


    if(fwrite($apns, $apnsMessage))
    {
        echo "Message send Successfully";
    }
    else
    {
        echo "Message Sending Fail";
    }
    fclose($apns);

    }
}

Here 3 IMP things to consider

1) Port 2195 must be enable on server

2) Certificate for particular application

3) Device token needed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.