如何实现iPhone Push Demo

 

PushMeBaby source code:

http://stefan.hafeneger.name/download/PushMeBabySource.zip

 

后面是app delegate class 代码实现可以参考Urban Airship:

http://bitbucket.org/urbanairship/push_sample/src/tip/APNS/Classes/APNSAppDelegate.m#cl-136

 

服务器实现:
<?php
$deviceToken = $_GET['deviceToken'];
echo $_GET['deviceToken'];
// Passphrase for the private key (ck.pem file)
// $pass = '';

// Get the parameters from http get or from command line
$message = $_GET['message'] or $message = $argv[1] or $message = 'You got a message';
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
$sound = $_GET['sound'] or $sound = $argv[3];

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
$body['aps']['badge'] = $badge;
if ($sound)
$body['aps']['sound'] = $sound;


/* End of Configurable Items */

$ctx = stream_context_create();
/*stream_context_set_option($ctx, 'ssl','allow_self_signed', true);
stream_context_set_option($ctx, 'ssl', 'verify_peer',false);*/
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
// assume the private key passphase was removed.
// stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);

$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr/n";
return;
}
else {
print "Connection OK/n";
}

$payload = json_encode($body);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "/n";
fwrite($fp, $msg);
fclose($fp);
?>

 

其他相关文档:

iPhone Software Development: Apple Push Notification Service (APNS)
iPhone Software Development: APNS Client Development Certificate Available Now
OpenSSL support in PHP under MAMP | Barry Jaspan
Running a secure web server on your Leopard-powered Mac | Michael Gracie

你可能感兴趣的:(Stream,ssl,iPhone,Parameters,FP,badge)