views:

1855

answers:

3

Hi.

I have a quick question. For the apple push notification service, I need to have my own server, which will send out the push notifications to the apple servers, right?

Can I do this with a simple PHP script on a regular webhosting account, or do I need a dedicated server with full blown admin access for that?

If this is possible in PHP, can anyone point me to some samples that can help me get started on this? Right now, I am pretty confident I won't have trouble implementing the client-side part, but the server side is still somewhat of a mystery to me...

Thank you!

Florian

A: 

You probably will be able to do this on a limited hosting account, as long as you can leave the connection open to the server most of the time. Some sample code:

http://code.google.com/p/php-apns/

Note also that some companies are starting up services to help you specifically with push hosting (I'll keep the post neutral and not mention names, I'm not sure which services are running just yet).

Kendall Helmstetter Gelner
+1  A: 

The Main problem with APNS is ports

so many providers doesnt open 2195 port

so concentrate on that initially then go for the host provider

Here is the code what i tried, but one problem is not able to get device notification

Hi ,

i tried the following code (PHP)

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

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

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
if($apns)
{
 echo "Connection Established<br/>";
 $deviceToken = '**********';//masked

 $body = array();
 $body['aps'] = array(’alert’ => "test message");
 //$body['aps']['badge'] = 1;

 $payload = json_encode($body);


 $apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
 print "sending message :" . $apnsMessage . "<br/>";
 print "sending payload :" . $payload . "<br/>";
 fwrite($apns, $apnsMessage);

}
else
{ 
 echo "Connection Failed";

 echo $errorString;
 echo $error;
}
socket_close($apns);
fclose($apns);

reply is Connection Established sending message :�� d^÷Îå0ZCd%1ÄuwOOYš'ÊÈ}ârðm¾Í�,{"aps":{"\u2019alert\u2019":"test message"}} sending payload :{"aps":{"\u2019alert\u2019":"test message"}}

But am not able to get the notification

any help?

Kamal Challa
A: 

Hi there,

@Kamal Challa zou probably solved your problem already but i had an idea: your certificate file *.pem is called produktion but you are using the development gateway. maybe you mixed something up and that is why it is not working?

but i have a problem myself. my code looks pretty much like yours but the connection fails. here is my code:

//open connection $apnsHost = 'gateway.sandbox.push.apple.com'; $apnsPort = 2195; $apnsCert = 'apns-dev.pem';

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

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

if($apns){ echo "Connection established"; } else{ echo "Connection failed"; }

If i execute the code it will always run into the else. Does anyone have an idea what i am doing wrong???

i would really appreciate any help!

katharina