当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Server Side iPhone APNS Communication

问题描述:

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

网友答案:

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).

网友答案:

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?

分享给朋友:
您可能感兴趣的文章:
随机阅读: